Intro to Optimization CS 671 February 28, 2008. CS 671 – Spring 2008 1 The Back End At this point we could generate machine code Output of lowering is.

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Code Generation & Optimization.
Advertisements

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
Lecture 11: Code Optimization CS 540 George Mason University.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Program Representations. Representing programs Goals.
The Last Lecture Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission.
Optimization Compiler Baojian Hua
Introduction to Advanced Topics Chapter 1 Mooly Sagiv Schrierber
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
Java for High Performance Computing Jordi Garcia Almiñana 14 de Octubre de 1998 de la era post-internet.
Introduction to Program Optimizations Chapter 11 Mooly Sagiv.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Precision Going back to constant prop, in what cases would we lose precision?
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
What’s in an optimizing compiler?
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
High-Level Transformations for Embedded Computing
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM John.
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
3/2/2016© Hal Perkins & UW CSES-1 CSE P 501 – Compilers Optimizing Transformations Hal Perkins Autumn 2009.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Credible Compilation With Pointers Martin Rinard and Darko Marinov Laboratory for Computer Science Massachusetts Institute of Technology.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
More Code Generation and Optimization Pat Morin COMP 3002.
CS 404 Introduction to Compiler Design
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Code Optimization Overview and Examples
Code Optimization.
Introduction to Advanced Topics Chapter 1 Text Book: Advanced compiler Design implementation By Steven S Muchnick (Elsevier)
ESE532: System-on-a-Chip Architecture
Optimization Code Optimization ©SoftMoore Consulting.
Machine-Independent Optimization
Optimizing Transformations Hal Perkins Autumn 2011
Optimizing Transformations Hal Perkins Winter 2008
Code Optimization Overview and Examples Control Flow Graph
9. Optimization Marcus Denker.
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Compiler Construction
Lecture 19: Code Optimisation
Loop-Level Parallelism
CSc 453 Final Code Generation
CSc 453 Interpreters & Interpretation
CMPE 152: Compiler Design April 30 Class Meeting
Code Optimization.
Presentation transcript:

Intro to Optimization CS 671 February 28, 2008

CS 671 – Spring The Back End At this point we could generate machine code Output of lowering is a correct translation What’s left to do? –Map from lower-level IR to machine code –Register management –Pass off to assembler Why have a separate assembler? Handles “packing the bits” Assembly addi,, Machine ss ssst tttt iiii iiii iiii iiii

CS 671 – Spring But First… The compiler “understands” the program IR captures program semantics Lowering: semantics-preserving transformation Compiler optimizations Now my program will be optimal! –Sorry, it’s a misnomer What is an “optimization”?

CS 671 – Spring Optimizations What are they? Code transformations Improve some metric Metrics Performance: time, instructions, cycles Space: Reduce memory usage Code Size Energy

CS 671 – Spring Why Optimize? High-level constructs may make some optimizations difficult or impossible: High-level code may be more desirable Program at high level Focus on design; clean, modular implementation Let compiler worry about gory details Premature optimization is the root of all evil! A[i][j] = A[i][j-1] + 1 t = A + i*row + j s = A + i*row + j – 1 (*t) = (*s) + 1

CS 671 – Spring Limitations What are optimizers good at? Being consistent and thorough Find all opportunities for an optimization Uniformly apply the transformation What are they not good at? Asymptotic complexity Compilers can’t fix bad algorithms Compilers can’t fix bad data structures There’s no magic

CS 671 – Spring Requirements Safety Preserve the semantics of the program Profitability Will it help our metric? Risk How will interact with other optimizations? How will it affect other stages of compilation?

CS 671 – Spring Example Loop Unrolling Safety: Always safe; getting loop conditions right can be tricky. Profitability Depends on hardware – usually a win Risk Increases size of code in loop May not fit in the instruction cache

CS 671 – Spring Optimizations Many, many optimizations invented Constant folding, constant propagation, tail-call elimination, redundancy elimination, dead code elimination, loop-invariant code motion, loop splitting, loop fusion, strength reduction, array scalarization, inlining, cloning, data prefetching, parallelization...etc.. How do they interact? Optimist: we get the sum of all improvements! Realist: many are in direct opposition

CS 671 – Spring Categories Traditional optimizations Transform the program to reduce work Don’t change the level of abstraction Enabling transformations Don’t necessarily improve code on their own Inlining, loop unrolling Resource allocation Map program to specific hardware properties Register allocation Instruction scheduling, parallelism Data streaming, prefetching

CS 671 – Spring Constant Propagation Idea If the value of a variable is known to be a constant at compile-time, replace the use of variable with constant Safety Prove the value is constant Notice: May interact favorably with other optimizations, like loop unrolling – now we know the trip count n = 10; c = 2; for (i=0;i<n;i++) s = s + i*c; n = 10; c = 2; for (i=0;i<10;i++) s = s + i*2;

CS 671 – Spring Constant Folding Idea If operands are known at compile-time, evaluate expression at compile-time Often repeated throughout compilation r = * 10;r = 31.41; x = A[2];t1 = 2*4; t2 = A + t1; x = *t2;

CS 671 – Spring Partial Evaluation Constant Propagation and Folding Together Idea: Evaluate as much of the program at compile-time as possible More sophisticated schemes: –Simulate data structures, arrays –Symbolic execution of the code Caveat: Floating Point Preserving the error characteristics of floating point values

CS 671 – Spring Algebraic Simplification Idea: Apply the usual algebraic rules to simplify expressions Repeatedly apply to complex expressions Many, many possible rules Associativity and commutativity come into play a * 1 a/1 a * 0 a + 0 b || false aa0abaa0ab

CS 671 – Spring Dead Code Elimination Idea: If the result of a computation is never used, then we can remove the computation Safety Variable is dead if it is never used after defined Remove code that assigns to dead variables This may, in turn, create more dead code Dead-code elimination usually works transitively x = y + 1; y = 1; x = 2 * z; y = 1; x = 2 * z;

CS 671 – Spring Common Sub-Expression Elimination Idea: If program computes the same expression multiple times, reuse the value. Safety: Subexpression can only be reused until operands are redefined Often occurs in address computations Array indexing and struct/field accesses a = b + c; c = b + c; d = b + c; t = b + c a = t; c = t; d = b + c;

CS 671 – Spring How Do These Things Happen? Who would write code with: Dead code Common subexpressions Constant expressions Copies of variables Two ways they occur High-level constructs – already saw examples Other optimizations –Copy propagation often leaves dead code –Enabling transformations: inlining, loop unrolling, etc.

CS 671 – Spring Copy Propagation Idea: After an assignment x = y, replace any uses of x with y Safety: Only apply up to another assignment to x, or …another assignment to y! What if there were an assignment y = z earlier? Apply transitively to all assignments x = y; if (x>1) s = x+f(x); x = y; if (y>1) s = y+f(y);

CS 671 – Spring Unreachable Code Elimination Idea: Eliminate code that can never be executed Different Implementations High-level: look for if (false) or while (false) Low-level: more difficult –Code is just labels and gotos –Traverse the graph, marking reachable blocks #define DEBUG 0... if (DEBUG) print(“Current value = “, v);

CS 671 – Spring Loop Optimizations Program hot-spots are usually in loops Most programs: 90% of execution time is in loops What are possible exceptions? OS kernels, compilers and interpreters Loops are a good place to expend extra effort Numerous loop optimizations Very effective Many are more expensive optimizations

CS 671 – Spring Loop-Invariant Code Motion Idea: If a computation won’t change from one loop iteration to the next, move it outside the loop Safety: Determine when expressions are invariant Useful for array address computations Not visible at source level for (i=0;i<N;i++) A[i] = A[i] + x*x; t1 = x*x; for (i=0;i<N;i++) A[i] = A[i] + t1;

CS 671 – Spring Strength Reduction Idea: Replace expensive operations (mult, div) with cheaper ones (add, sub, bit shift) Traditionally applied to induction variables Variables whose value depends linearly on loop count Special analysis to find such variables for (i=0;i<N;i++) v = 4*i; A[v] =... v = 0; for (i=0;i<N;i++) A[v] =... v = v + 4;

CS 671 – Spring Strength Reduction Can also be applied to simple arithmetic operations: Typical example of premature optimization Programmers use bit-shift instead of multiplication “x<<2” is harder to understand Most compilers will get it right automatically x * 2 x * 2^c x/2^c x + x x<<c x>>c

CS 671 – Spring Inlining Idea: Replace a function call with the body of the callee Safety What about recursion? Risk Code size Most compilers use heuristics to decide when Has been cast as a knapsack problem Critical for OO languages Methods are often small Encapsulation, modularity force code apart

CS 671 – Spring Inlining In C: At a call-site, decide whether or not to inline –(Often a heuristic about callee/caller size) Look up the callee Replace call with body of callee What about Java? What complicates this? Virtual methods Even worse? Dynamic class loading class A { void M() {…} } class B extends A { void M() {…} } void foo(A x) { x.M(); // which M? }

CS 671 – Spring Inlining in Java With guards: Specialization At a given call, we may be able to determine the type Requires fancy analysis void foo(A x) { if (x is type A) x.M(); // inline A’s M if (x is type B) x.M(); // inline B’s M } y = new A(); foo(y); z = new B(); foo(z);

CS 671 – Spring Control-Flow Simplification High-level optimization Idea: If we know the value of a branch condition, eliminate the unused branch How would that happen? Combination of other opts: –Constant propagation, constant folding What’s the benefit? Straight-line code Easier to reason about, easier to optimize Better for pipelined architectures if (10 > 5) {... } else {... }

CS 671 – Spring Anatomy of an Optimization Two big parts: Program analysis - Pass over code to find: Opportunities Satisfy safety constraints Program transformation Change the code to exploit opportunity

CS 671 – Spring Big Picture When do we apply these optimizations? High-level: –Inlining, cloning –Some algebraic simplifications Low-level –Everything else It’s a black art Ordering is often arbitrary Many compilers just repeat the optimization passes over and over

CS 671 – Spring Overview Front endLoweringBack end Optimize High-level optimizations – take advantage of AST Optimize Low-level optimizations – expose implementation

CS 671 – Spring Writing Fast Programs In practice: Pick the right algorithms and data structures Asymptotic complexity Memory usage, indirection, representation Turn on optimization and profile Run-time Program counters (e.g., cache misses) Evaluate problems Tweak source code Make the optimizer do “the right thing”

CS 671 – Spring Summary Myriad optimizations to improve programs – particularly runtime Optimizations interact in both positive and negative ways Primary issue: safety

CS 671 – Spring After the Break … Program analysis Dead-code elimination