cs33-optimization

cs33-optimization - Code Optimization I: Code Optimization...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Code Optimization I: Code Optimization I: Machine Independent Optimizations Machine Independent Optimizations Chapter 5 of B&O Some notes adopted from Bryant and OHallaron Great Reality #4 Great Reality #4 Theres more to performance than asymptotic complexity Constant factors matter too! Easily see 10:1 performance range depending on how code is written ust optimize at multiple levels: Must optimize at multiple levels: algorithm, data representations, procedures, and loops ust understand system to optimize performance Must understand system to optimize performance How programs are compiled and executed How to measure program performance and identify bottlenecks How to improve performance without destroying code modularity and generality Optimizing Compilers Optimizing Compilers Provide efficient mapping of program to machine register allocation code selection and ordering dead code elimination liminating minor inefficiencies eliminating minor inefficiencies Dont (usually) improve asymptotic efficiency p to programmer to select best overall algorithm up to programmer to select best overall algorithm big O savings are (often) more important than constant factors but constant factors also matter Have difficulty overcoming optimization blockers potential memory aliasing potential procedure side effects Limitations of Optimizing Compilers Limitations of Optimizing Compilers Operate Under Fundamental Constraint Must not cause any change in program behavior under any ossible condition possible condition Often prevents it from making optimizations when would only affect behavior under pathological conditions. Behavior that may be obvious to the programmer can be obfuscated by languages and coding styles g data ranges may be more limited than variable types suggest e.g., data ranges may be more limited than variable types suggest Most analysis is performed only within procedures whole program analysis is too expensive in most cases Most analysis is based only on static information compiler has difficulty anticipating run time inputs When in doubt, the compiler must be conservative Machine Machine-Independent Optimizations Independent Optimizations Optimizations you should do regardless of processor / compiler Code Motion educe frequency with which computation performed Reduce frequency with which computation performed If it will always produce same result Especially moving code out of loop for (i = 0; i < n; i++) for (i = 0; i < n; i++) { int ni = n*i; for (j = 0; j < n; j++) a[n*i + j] = b[j]; for (j = 0; j < n; j++) a[ni + j] = b[j]; } Compiler Compiler-Generated Code Motion Generated Code Motion Most compilers do a good job with array code + simple loop structures Code Generated by GCC for (i = 0; i < n; i++) for (i = 0; i < n; i++) { int ni = n*i; int *p = a+ni; for (j = 0; j < n; j++)...
View Full Document

This note was uploaded on 06/17/2011 for the course CS 33 taught by Professor Rohr during the Spring '08 term at UCLA.

Page1 / 32

cs33-optimization - Code Optimization I: Code Optimization...

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

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