3/2/2016© 2002-09 Hal Perkins & UW CSES-1 CSE P 501 – Compilers Optimizing Transformations Hal Perkins Autumn 2009.

Slides:



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

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
Computer Architecture Lecture 7 Compiler Considerations and Optimizations.
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.
The Last Lecture Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission.
Loop Invariant Code Motion — classical approaches — 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Peephole Optimization Final pass over generated code: examine a few consecutive instructions: 2 to 4 See if an obvious replacement is possible: store/load.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
Introduction to Program Optimizations Chapter 11 Mooly Sagiv.
Case Studies of Compilers and Future Trends Chapter 21 Mooly Sagiv.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Data-Flow Analysis (Chapter 11-12) Mooly Sagiv Make-up class 18/ :00 Kaplun 324.
Hardware-Software Interface Machine Program Performance = t cyc x CPI x code size X Available resources statically fixed Designed to support wide variety.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Spring 2014Jim Hogg - UW - CSE - P501S-1 CSE P501 – Compiler Construction Optimizing transformations : a sampler Will dig deeper later, after analyzing.
Optimizing Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
What’s in an optimizing compiler?
1 Advance Computer Architecture CSE 8383 Ranya Alawadhi.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Order from Chaos — the big picture — 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Order from Chaos — the big picture — C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Dependence Analysis and Loop Transformations.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
High-Level Transformations for Embedded Computing
Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation:  Have each value in a register when it is used. Instruction selection.
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.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
L9 : Low Power DSP Jun-Dong Cho SungKyunKwan Univ. Dept. of ECE, Vada Lab.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Intro to Optimization CS 671 February 28, CS 671 – Spring The Back End At this point we could generate machine code Output of lowering is.
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
Introduction to Optimization
High-level optimization Jakub Yaghob
Code Optimization.
Optimization Code Optimization ©SoftMoore Consulting.
Princeton University Spring 2016
Machine-Independent Optimization
Introduction to Optimization
Code Generation Part III
Optimizing Transformations Hal Perkins Autumn 2011
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
Optimizing Transformations Hal Perkins Winter 2008
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Introduction to Optimization
9. Optimization Marcus Denker.
Dynamic Hardware Prediction
Compiler Construction
Lecture 19: Code Optimisation
How to improve (decrease) CPI
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Introduction to Optimization
Code Optimization.
Presentation transcript:

3/2/2016© Hal Perkins & UW CSES-1 CSE P 501 – Compilers Optimizing Transformations Hal Perkins Autumn 2009

3/2/2016© Hal Perkins & UW CSES-2 Agenda A sampler of typical optimizing transformations Mostly a teaser for more details later, particularly once we’ve looked at analyzing loops

3/2/2016© Hal Perkins & UW CSES-3 Role of Transformations Data-flow analysis discovers opportunities for code improvement Compiler must rewrite the code (IR) to realize these improvements A transformation may reveal additional opportunities for further analysis & transformation May also block opportunities by obscuring information

3/2/2016© Hal Perkins & UW CSES-4 Organizing Transformations in a Compiler Typically middle end consists of many individual transformations that filter the IR and produce rewritten IR No systematic theory for the order to apply them Some rules of thumb and best practices Some transformations can be profitably applied repeatedly, particularly if others transformations expose more opportunities

3/2/2016© Hal Perkins & UW CSES-5 A Taxonomy Machine Independent Transformations Realized profitability may actually depend on machine architecture, but are typically implemented without considering this Machine Dependent Transformations Most of the machine dependent code is in instruction selection & scheduling and register allocation Some machine dependent code belongs in the optimizer

3/2/2016© Hal Perkins & UW CSES-6 Machine Independent Transformations Dead code elimination Code motion Specialization Strength reduction Enable other transformations Eliminate redundant computations Value numbering, GCSE

3/2/2016© Hal Perkins & UW CSES-7 Machine Dependent Transformations Take advantage of special hardware Expose instruction-level parallelism, for example Manage or hide latencies Improve cache behavior Deal with finite resources

3/2/2016© Hal Perkins & UW CSES-8 Dead Code Elimination If a compiler can prove that a computation has no external effect, it can be removed Useless operations Unreachable operations Dead code often results from other transformations Often want to do DCE several times

3/2/2016© Hal Perkins & UW CSES-9 Dead Code Elimination Classic algorithm is similar to garbage collection Pass I – Mark all useful operations Start with critical operations – output, entry/exit blocks, calls to other procedures, etc. Mark all operations that are needed for critical operations; repeat until convergence Pass II – delete all unmarked operations Note: need to treat jumps carefully

3/2/2016© Hal Perkins & UW CSES-10 Code Motion Idea: move an operation to a location where it is executed less frequently Classic situation: move loop-invariant code out of a loop and execute it once, not once per iteration Lazy code motion: code motion plus elimination of redundant and partially redundant computations

3/2/2016© Hal Perkins & UW CSES-11 Specialization Idea: Analysis phase may reveal information that allows a general operation in the IR to be replaced by a more specific one Constant folding Replacing multiplications and division by constants with shifts Peephole optimizations Tail recursion elimination

3/2/2016© Hal Perkins & UW CSES-12 Strength Reduction Classic example: Array references in a loop for (k = 0; k < n; k++) a[k] = 0; Simple code generation would usually produce address arithmetic including a multiplication (k*elementsize) and addition

3/2/2016© Hal Perkins & UW CSES-13 Implementing Strength Reduction Idea: look for operations in a loop involving: A value that does not change in the loop, the region constant, and A value that varies systematically from iteration to iteration, the induction variable Create a new induction variable that directly computes the sequence of values produced by the original one; use an addition in each iteration to update the value

3/2/2016© Hal Perkins & UW CSES-14 Some Enabling Transformations Already mentioned Inline substitution (procedure bodies) Block cloning A few other examples Loop Unrolling Loop Unswitching

3/2/2016© Hal Perkins & UW CSES-15 Loop Unrolling Idea: Replicate the loop body to expose inter-iteration optimization possibilities Increases chances for good schedules and instruction level parallelism Reduces loop overhead Catch – need to handle dependencies between iterations carefully

3/2/2016© Hal Perkins & UW CSES-16 Loop Unrolling Example Original for (i=1, i<=n, i++) a[i] = b[i]; Unrolled by 4 i=1; while (i+3 <= n) { a[i ] = a[i ]+b[i ]; a[i+1] = a[i+1]+b[i+1]; a[i+2] = a[i+2]+b[i+2]; a[i+3] = a[i+3]+b[i+3]; a+=4; } while (i <= n) { a[i] = a[i]+b[i]; i++; }

3/2/2016© Hal Perkins & UW CSES-17 Loop Unswitching Idea: if the condition in an if-then-else is loop invariant, rewrite the loop by pulling the if-then-else out of the loop and generating a tailored copy of the loop for each half of the new if After this transformation, both loops have simpler control flow – more chances for rest of compiler to do better

3/2/2016© Hal Perkins & UW CSES-18 Loop UnswitchingExample Original for (i=1, i<=n, i++) if (x > y) a[i] = b[i]*x; else a[i] = b[i]*y Unswitched if (x > y) for (i = 1; i < n; i++) a[i] = b[i]*x; else a[i] = b[i]*y;

3/2/2016© Hal Perkins & UW CSES-19 Summary This is just a sampler Hundreds of transformations in the literature We will look at several in more detail, particularly involving loops Big part of engineering a compiler is to decide which transformations to use, in what order, and when to repeat them Different tradeoffs depending on compiler goals