CS553 Compiler Construction Plan for Today Instructor: Michelle Strout Introductions mstrout@cs.colostate.edu USC 227 Motivation Office hours: decide in class – Why study compilers? URL: http://www.cs.colostate.edu/~cs553 Issues – Look at some sample optimizations and assorted issues Administrivia – Course details CS553 Lecture 1 Introduction 2 CS553 Lecture 1 Introduction 3 Motivation Traditional View of Compilers What is a compiler? Compiling down – A translator that converts a source program into an target program – Translate high-level language to machine code What is an optimizing compiler? High-level programming languages – A translator that somehow improves the program – Increase programmer productivity – Improve program maintenance Why study compilers? – Improve portability – They are specifically important: Compilers provide a bridge between applications and architectures Low-level architectural details – They are generally important: – Instruction set Compilers encapsulate techniques for reasoning about programs and their behavior – Addressing modes – They are cool: – Pipelines First major computer application – Registers, cache, and the rest of the memory hierarchy – Instruction-level parallelism CS553 Lecture 1 Introduction 4 CS553 Lecture 1 Introduction 5 CS553 Lecture 1 1
Isn’t Compilation A Solved Problem? Modern View of Compilers Applications keep changing “Optimization for scalar machines is a Analysis and translation are useful everywhere problem that was solved ten years ago” – Interactive, real-time, mobile, – Analysis and transformations can be performed at run time and link time, secure -- David Kuck, 1990 not just at “compile time” – Optimization can be applied to OS as well as applications Some apps always want more Machines keep changing – Analysis can be used to improve security by finding bugs – More accuracy – New features present new problems – Analysis can be used in software engineering – Simulate larger systems ( e.g., MMX, EPIC, profiling – Program understanding, reverse engineering, refactoring support) – Debugging and testing Goals keep changing – Changing costs lead to different – Increased interaction between hardware and compilers can improve – Correctness concerns ( e.g., loads) performance – Run-time performance – Bottom line – Code size Languages keep changing – Analysis and transformation play essential roles in computer systems – Compile-time performance – Wacky ideas ( e.g., OOP and GC) – Computation important � understanding computation important – Power have gone mainstream – Security CS553 Lecture 1 Introduction 6 CS553 Lecture 1 Introduction 7 Types of Optimizations Types of Optimizations (cont) Definition Machine-dependent optimizations – An optimization is a transformation that is expected to improve the – Replace costly operation with cheaper one program in some way; often consists of analysis and transformation – Replace sequence of operations with cheaper one e.g., decreasing the running time or decreasing memory requirements – Hide latency – Improve locality Machine-independent optimizations – Exploit machine parallelism – Eliminate redundant computation – Reduce power consumption – Move computation to less frequently executed place – Specialize some general purpose code Enabling transformations – Remove useless code – Expose opportunities for other optimizations – Help structure optimizations CS553 Lecture 1 Introduction 8 CS553 Lecture 1 Introduction 9 CS553 Lecture 1 2
Sample Optimizations Sample Optimizations (cont) Arithmetic simplification Common subexpression elimination (CSE) – e.g., – Constant folding x = a + b; t = a + b; e.g., x = 8/2; x = 4; y = a + b; x = t; y = t; – Strength reduction e.g., x = y * 4; x = y << 2; Dead (unused) assignment elimination – e.g., x = 3; Constant propagation ... x not used... this assignment is dead x = 4; – e.g., x = 3; x = 3; x = 3; y = 4+x; y = 4+3; y = 7; Dead (unreachable) code elimination this statement is dead Copy propagation – e.g., if (false == true) { printf(“debugging...”); – e.g., x = z; x = z; } y = 4+x; y = 4+z; CS553 Lecture 1 Introduction 10 CS553 Lecture 1 Introduction 11 Sample Optimizations (cont) Is an Optimization Worthwhile? Loop-invariant code motion Criteria for evaluating optimizations x = 3; – e.g., for i = 1 to 10 do – Safety: does it preserve behavior? for i = 1 to 10 do x = 3; – Profitability: does it actually improve the code? ... ... – Opportunity: is it widely applicable? – Cost (compilation time): can it be practically performed? Induction variable elimination – Cost (complexity): can it be practically implemented? – e.g., for i = 1 to 10 do for p = &a[1] to &a[10] do a[i] = a[i] + 1; *p = *p + 1 Loop unrolling for i = 1 to 10 by 2 do – e.g., for i = 1 to 10 do a[i] = a[i] + 1; a[i] = a[i] + 1; a[i+1] = a[i+1] + 1; CS553 Lecture 1 Introduction 12 CS553 Lecture 1 Introduction 13 CS553 Lecture 1 3
Scope of Analysis/Optimizations Limits of Compiler Optimizations Fully Optimizing Compiler (FOC) Peephole Global (intraprocedural) – FOC(P) = P opt – Consider a small window of – Consider entire procedures instructions – P opt is the smallest program with same I/O behavior as P – Must consider branches, loops, – Usually machine specific merging of control flow Observe – Use data-flow analysis – If program Q produces no output and never halts, FOC(Q) = – Make simplifying assumptions at L: goto L procedure calls Aha! Whole program (interprocedural) Local – We’ve solved the halting problem?! – Consider multiple procedures – Consider blocks of straight line code (no control flow) – Analysis even more complex Moral (calls, returns) – Simple to analyze – Cannot build FOC – Hard with separate compilation – Can always build a better optimizing compiler ( full employment theorem for compiler writers!) CS553 Lecture 1 Introduction 14 CS553 Lecture 1 Introduction 15 Optimizations Don’t Always Help Optimizations Don’t Always Help (cont) Common Subexpression Elimination Backpatching x = a + b t = a + b In Java, the address of foo() is often not known until o.foo(); y = a + b x = t runtime (due to dynamic class loading), so the method call requires a table lookup. y = t After the first execution of this statement, backpatching 2 adds 1 add replaces the table lookup with a direct call to the proper 4 variables 5 variables function. Q: How could this optimization ever hurt? A: The Pentium 4 has a trace cache, when any instruction is modified, the entire trace cache has to be flushed. CS553 Lecture 1 Introduction 16 CS553 Lecture 1 Introduction 18 CS553 Lecture 1 4
Phase Ordering Problem Engineering Issues In what order should optimizations be performed? Building a compiler is an engineering activity Simple dependences Balance multiple goals – One optimization creates opportunity for another – Benefit for typical programs e.g., copy propagation and dead code elimination – Complexity of implementation – Compilation speed Cyclic dependences – e.g., constant folding and constant propagation Overall Goal – Identify a small set of general analyses and optimization Adverse interactions – Easier said than done: just one more... – e.g., common subexpression elimination and register allocation e.g., register allocation and instruction scheduling CS553 Lecture 1 Introduction 19 CS553 Lecture 1 Introduction 20 Beyond Optimization Administrative Matters Security and Correctness Turn to your syllabus – Can we check whether pointers and addresses are valid? – Can we detect when untrusted code accesses a sensitive part of a system? – Can we detect whether locks are used properly? – Can we use compilers to certify that code is correct? – Can we use compilers to obfuscate code? CS553 Lecture 1 Introduction 21 CS553 Lecture 1 Introduction 22 CS553 Lecture 1 5
Next Time Concepts Reading Language implementation is interesting – Intro material in Appel and in SableCC manual Optimal in name only Optimization scope Lecture – Peephole, local, global, whole program – Scanning and parsing review Optimizations – Arithmetic simplification (constant folding, strength reduction) – Constant/copy propagation – Common subexpression elimination – Dead assignment/code elimination – Loop-invariant code motion – Induction variable elimination – Loop unrolling Phase ordering problem CS553 Lecture 1 Introduction 23 CS553 Lecture 1 Introduction 24 CS553 Lecture 1 6
Recommend
More recommend