USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

Similar documents
Office Hours: Mon/Wed 3:30-4:30 GDC Office Hours: Tue 3:30-4:30 Thu 3:30-4:30 GDC 5.

Computer Science Building 342 Office hours: decide in class

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Undergraduate Compilers in a Day

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization?

Tour of common optimizations

CS 553: Algorithmic Language Compilers (PLDI) Graduate Students and Super Undergraduates... Logistics. Plan for Today

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013

Code Optimization. Code Optimization

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Goals of Program Optimization (1 of 2)

Introduction to Compilers

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

Machine-Independent Optimizations

Languages and Compiler Design II IR Code Optimization

CS553 Lecture Dynamic Optimizations 2

Intermediate Code & Local Optimizations. Lecture 20

Intermediate Code & Local Optimizations

CS553 Lecture Profile-Guided Optimizations 3

Field Analysis. Last time Exploit encapsulation to improve memory system performance

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Loops. Announcements. Loop fusion. Loop unrolling. Code motion. Array. Good targets for optimization. Basic loop optimizations:

Compiler Optimization

Advanced Compiler Construction

Administration. Prerequisites. Meeting times. CS 380C: Advanced Topics in Compilers

Compiler Optimization and Code Generation

CS153: Compilers Lecture 15: Local Optimization

Multi-dimensional Arrays

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Running class Timing on Java HotSpot VM, 1

Operational Semantics of Cool

Code Merge. Flow Analysis. bookkeeping

Programming Language Processor Theory

CSC D70: Compiler Optimization

Compilers and Interpreters

Register Allocation. CS 502 Lecture 14 11/25/08

Lecture 9 Dynamic Compilation

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CS 406/534 Compiler Construction Putting It All Together

Using Static Single Assignment Form

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

Lecture Notes on Loop Optimizations

Supercomputing in Plain English Part IV: Henry Neeman, Director

Introduction to optimizations. CS Compiler Design. Phases inside the compiler. Optimization. Introduction to Optimizations. V.

Compilers. Lecture 2 Overview. (original slides by Sam

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

Compiler construction 2009

Compiler Construction 2010/2011 Loop Optimizations

Optimization Prof. James L. Frankel Harvard University

ECE 486/586. Computer Architecture. Lecture # 7

Usually, target code is semantically equivalent to source code, but not always!

More Code Generation and Optimization. Pat Morin COMP 3002

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction

Compiler Construction 2016/2017 Loop Optimizations

IR Optimization. May 15th, Tuesday, May 14, 13

Tiling: A Data Locality Optimizing Algorithm

Control flow graphs and loop optimizations. Thursday, October 24, 13

Background: Pipelining Basics. Instruction Scheduling. Pipelining Details. Idealized Instruction Data-Path. Last week Register allocation

Induction Variable Identification (cont)

16.10 Exercises. 372 Chapter 16 Code Improvement. be translated as

CS 132 Compiler Construction

Overview of a Compiler

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Introduction to Machine-Independent Optimizations - 1

The View from 35,000 Feet

CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II

Calvin Lin The University of Texas at Austin

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Control Flow Analysis. Reading & Topics. Optimization Overview CS2210. Muchnick: chapter 7

Compiler Optimization Intermediate Representation

CprE 488 Embedded Systems Design. Lecture 6 Software Optimization

Under the Compiler's Hood: Supercharge Your PLAYSTATION 3 (PS3 ) Code. Understanding your compiler is the key to success in the gaming world.

Intra-procedural Data Flow Analysis Introduction

Simone Campanoni Loop transformations

Loop Transformations! Part II!

Introduction to Optimization Local Value Numbering

Compiler Optimization Techniques

Module 13: INTRODUCTION TO COMPILERS FOR HIGH PERFORMANCE COMPUTERS Lecture 25: Supercomputing Applications. The Lecture Contains: Loop Unswitching

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

Topic I (d): Static Single Assignment Form (SSA)

What Compilers Can and Cannot Do. Saman Amarasinghe Fall 2009

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Resource-aware Computing Lecture 1- Grading, Rules and Introduction

Preface. Intel Technology Journal Q4, Lin Chao Editor Intel Technology Journal

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

IA-64 Compiler Technology

CSE 373 OCTOBER 25 TH B-TREES

Systems I. Code Optimization I: Machine Independent Optimizations

CSCI 565 Compiler Design and Implementation Spring 2014

Transcription:

CS553 Compiler Construction Instructor: URL: Michelle Strout mstrout@cs.colostate.edu USC 227 Office hours: 3-4 Monday and Wednesday http://www.cs.colostate.edu/~cs553 CS553 Lecture 1 Introduction 3 Plan for Today Motivation Why study compilers? Issues Look at some sample optimizations and assorted issues Administrivia Course details CS553 Lecture 1 Introduction 4 CS553 Lecture 1 1

Motivation What is a compiler? A translator that converts a source program into an target program What is an optimizing compiler? A translator that somehow improves the program Why study compilers? They are specifically important: Compilers provide a bridge between applications and architectures They are generally important: Compilers encapsulate techniques for reasoning about programs and their behavior They are cool: First major computer application CS553 Lecture 1 Introduction 5 Traditional View of Compilers Compiling down Translate high-level language to machine code High-level programming languages Increase programmer productivity Improve program maintenance Improve portability Low-level architectural details Instruction set Addressing modes Pipelines Registers, cache, and the rest of the memory hierarchy Instruction-level parallelism CS553 Lecture 1 Introduction 6 CS553 Lecture 1 2

Isn t Compilation A Solved Problem? Optimization for scalar machines is a problem that was solved ten years ago -- David Kuck, 1990 Machines keep changing New features present new problems (e.g., MMX, EPIC, profiling support) Changing costs lead to different concerns (e.g., loads) Languages keep changing Wacky ideas (e.g., OOP and GC) have gone mainstream Applications keep changing Interactive, real-time, mobile, secure Some apps always want more More accuracy Simulate larger systems Goals keep changing Correctness Run-time performance Code size Compile-time performance Power Security CS553 Lecture 1 Introduction 7 Modern View of Compilers Analysis and translation are useful everywhere Analysis and transformations can be performed at run time and link time, not just at compile time Optimization can be applied to OS as well as applications Translation can be used to improve security Analysis can be used in software engineering Program understanding Reverse engineering Increased interaction between hardware and compilers can improve performance Bottom line Analysis and transformation play essential roles in computer systems Computation important understanding computation important CS553 Lecture 1 Introduction 8 CS553 Lecture 1 3

Types of Optimizations Definition An optimization is a transformation that is expected to improve the program in some way; often consists of analysis and transformation e.g., decreasing the running time or decreasing memory requirements Machine-independent optimizations Eliminate redundant computation Move computation to less frequently executed place Specialize some general purpose code Remove useless code CS553 Lecture 1 Introduction 9 Types of Optimizations (cont) Machine-dependent optimizations Replace costly operation with cheaper one Replace sequence of operations with cheaper one Hide latency Improve locality Exploit machine parallelism Reduce power consumption Enabling transformations Expose opportunities for other optimizations Help structure optimizations CS553 Lecture 1 Introduction 10 CS553 Lecture 1 4

Sample Optimizations Arithmetic simplification Constant folding e.g., x = 8/2; x = 4; Strength reduction e.g., x = y * 4; x = y << 2; Constant propagation e.g., x = 3; x = 3; x = 3; y = 4+x; y = 4+3; y = 7; Copy propagation e.g., x = z; x = z; y = 4+x; y = 4+z; CS553 Lecture 1 Introduction 11 Sample Optimizations (cont) Common subexpression elimination (CSE) e.g., x = a + b; y = a + b; t = a + b; x = t; y = t; Dead (unused) assignment elimination e.g., x = 3;... x not used... x = 4; this assignment is dead Dead (unreachable) code elimination e.g., if (false == true) { printf( debugging... ); } this statement is dead CS553 Lecture 1 Introduction 12 CS553 Lecture 1 5

Sample Optimizations (cont) Loop-invariant code motion e.g., for i = 1 to 10 do x = 3;... x = 3; for i = 1 to 10 do... Induction variable elimination e.g., for i = 1 to 10 do a[i] = a[i] + 1; Loop unrolling e.g., for i = 1 to 10 do a[i] = a[i] + 1; for p = &a[1] to &a[10] do *p = *p + 1 for i = 1 to 10 by 2 do a[i] = a[i] + 1; a[i+1] = a[i+1] + 1; CS553 Lecture 1 Introduction 13 Is an Optimization Worthwhile? Criteria for evaluating optimizations Safety: does it preserve behavior? Profitability: does it actually improve the code? Opportunity: is it widely applicable? Cost (compilation time): can it be practically performed? Cost (complexity): can it be practically implemented? CS553 Lecture 1 Introduction 14 CS553 Lecture 1 6

Scope of Analysis/Optimizations Peephole Consider a small window of instructions Usually machine specific Local Consider blocks of straight line code (no control flow) Simple to analyze Global (intraprocedural) Consider entire procedures Must consider branches, loops, merging of control flow Use data-flow analysis Make simplifying assumptions at procedure calls Whole program (interprocedural) Consider multiple procedures Analysis even more complex (calls, returns) Hard with separate compilation CS553 Lecture 1 Introduction 15 Limits of Compiler Optimizations Fully Optimizing Compiler (FOC) FOC(P) = P opt P opt is the smallest program with same I/O behavior as P Observe If program Q produces no output and never halts, FOC(Q) = L: goto L Aha! We ve solved the halting problem?! Moral Cannot build FOC Can always build a better optimizing compiler (full employment theorem for compiler writers!) CS553 Lecture 1 Introduction 16 CS553 Lecture 1 7

Optimizations Don t Always Help Common Subexpression Elimination x = a + b t = a + b y = a + b x = t y = t 2 adds 1 add 4 variables 5 variables CS553 Lecture 1 Introduction 17 Optimizations Don t Always Help (cont) Fusion and Contraction for i = 1 to n T[i] = A[i] + B[i] for i = 1 to n C[i] = D[i] + T[i] for i = 1 to n t = A[i] + B[i] C[i] = D[i] + t t fits in a register, so no loads or stores in this loop. Huge win on most machines. Degrades performance on machines with hardware managed stream buffers. CS553 Lecture 1 Introduction 18 CS553 Lecture 1 8

Optimizations Don t Always Help (cont) Backpatching o.foo(); In Java, the address of foo() is often not known until runtime (due to dynamic class loading), so the method call requires a table lookup. After the first execution of this statement, backpatching replaces the table lookup with a direct call to the proper 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 19 Phase Ordering Problem In what order should optimizations be performed? Simple dependences One optimization creates opportunity for another e.g., copy propagation and dead code elimination Cyclic dependences e.g., constant folding and constant propagation Adverse interactions e.g., common subexpression elimination and register allocation e.g., register allocation and instruction scheduling CS553 Lecture 1 Introduction 20 CS553 Lecture 1 9

Engineering Issues Building a compiler is an engineering activity Balance multiple goals Benefit for typical programs Complexity of implementation Compilation speed Overall Goal Identify a small set of general analyses and optimization Easier said than done: just one more... CS553 Lecture 1 Introduction 21 Beyond Optimization Security and Correctness 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 22 CS553 Lecture 1 10

Administrative Matters Turn to your syllabus CS553 Lecture 1 Introduction 23 Next Time Reading Intro material in Muchnick and in Bison manual Lecture Scanning and parsing review CS553 Lecture 1 Introduction 24 CS553 Lecture 1 11

Concepts Language implementation is interesting Optimal in name only Optimization scope Peephole, local, global, whole program 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 25 CS553 Lecture 1 12