c14 - CS421 COMPILERS AND INTERPRETERS CS421 Code...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Code Optimizations: Page 1 of 15 Code Optimizations • The intermediate code (e.g., IR tree) generated by the front-end is often not efficient . •T h e code optimizer reads IR, emits better IR; almost all optimizations done here are machine-independent . Machine-dependent optimizations are done in the back-end. • Main techniques used: graph algorithms, control- and data- flow analysis compiler front-end code optimizer compiler back-end source program better intermediate code target machine code intermediate code CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Code Optimizations: Page 2 of 15 Code Optimizations (cont’d) •A code optimizer is often organized as follows: Control-Flow Analysis --- divide the IR into basic blocks, build the control-flow graph (CFG) • Data-Flow Analysis --- gather data-flow information (e.g., the set of live variables). • Code Transformations --- the actual optimizations control-flow analysis data-flow analysis many code transformations intermediate code inter. code with control-flow info inter. code with data-flow info. improved inter. code CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Code Optimizations: Page 3 of 15 Code Optimizations (cont’d) • Optimizations that are restricted to one basic block are called local- optimizations ; otherwise, they are called global optimizations • Here are a partial list of well-known compiler optimizations: algebraic optimizations (strength reduction, constant folding) common-subexpression eliminations copy propagations and constant propagations dead-code eliminations code-motions (i.e., lifting loop-invariants) induction variable eliminations; strength reductions for loops CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Code Optimizations: Page 4 of 15 Examples: Source Code • C code for quicksort (also in ASU page 588) : 1 void quicksort(m, n); 2 int m, n; 3 { 4 int i, j, v, x; 5 if (n <= m) return ; 6 7 i = m-1; j = n; v = a[n]; 8 while (1) { 9 do i = i+1; while ( a[i] < v); 10 do j = j-1; while ( a[j] > v); 11 if (i >= j) break ; 12 x = a[i]; a[i] = a[j]; a[j] = x; 13 } 14 x = a[i]; a[i] = a[n]; a[n] = x; 15 16
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Code Optimizations: Page 5 of 15 Example: Intermediate Code • Intermediate code for the shaded fragments of previous example: (01) i := m - 1 (02) j := n (03) t1 := 4 * n (04) v := a[t1] (05) i := i + 1 (06) t2 := 4 * i (07) t3 := a[t2] (08) if t3 < v goto (5) (09) j := j - 1 (10) t4 := 4 * j
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 01/06/2012.

Page1 / 4

c14 - CS421 COMPILERS AND INTERPRETERS CS421 Code...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online