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

Similar documents
CSC D70: Compiler Optimization

Lecture 1 Introduc-on

Lecture 3 Local Optimizations, Intro to SSA

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

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

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

Tour of common optimizations

Intermediate Code & Local Optimizations. Lecture 20

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

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

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

CODE GENERATION Monday, May 31, 2010

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

Languages and Compiler Design II IR Code Optimization

ECE 486/586. Computer Architecture. Lecture # 7

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

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Intermediate Code & Local Optimizations

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

Lecture Notes on Loop Optimizations

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

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

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

ICS 252 Introduction to Computer Design

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

Compiler Optimization and Code Generation

CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2]

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Programming Language Processor Theory

COMPILER DESIGN - CODE OPTIMIZATION

Machine-Independent Optimizations

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

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

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

Goals of Program Optimization (1 of 2)

Optimization Prof. James L. Frankel Harvard University

Compiler Construction SMD163. Understanding Optimization: Optimization is not Magic: Goals of Optimization: Lecture 11: Introduction to optimization

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

Code Optimization. Code Optimization

A main goal is to achieve a better performance. Code Optimization. Chapter 9

Static Single Assignment Form in the COINS Compiler Infrastructure

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Compiler Optimization

Introduction to Compilers

Multi-dimensional Arrays

Lecture Notes on Loop-Invariant Code Motion

Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu.

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

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

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

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?

Program Optimization. Jo, Heeseung

FSU. Av oiding Unconditional Jumps by Code Replication. Frank Mueller and David B. Whalley. Florida State University DEPARTMENT OF COMPUTER SCIENCE

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

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

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

Running class Timing on Java HotSpot VM, 1

Introduction to Optimization Local Value Numbering

CSEE 3827: Fundamentals of Computer Systems

Compiler Optimization Intermediate Representation

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

CS 6353 Compiler Construction, Homework #3

Cache Memories. Lecture, Oct. 30, Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Outline. Register Allocation. Issues. Storing values between defs and uses. Issues. Issues P3 / 2006

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

CS 2461: Computer Architecture 1

Compiler construction 2009

CS153: Compilers Lecture 15: Local Optimization

Compilers. Lecture 2 Overview. (original slides by Sam

Compiler Optimization Techniques

Supercomputing in Plain English Part IV: Henry Neeman, Director

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

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

Code Merge. Flow Analysis. bookkeeping

(just another operator) Ambiguous scalars or aggregates go into memory

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

Intermediate representation

CSE P 501 Compilers. Loops Hal Perkins Spring UW CSE P 501 Spring 2018 U-1

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

Intermediate Representations

Code generation and local optimization

Communicating with People (2.8)

CSCI 402: Computer Architectures. Instructions: Language of the Computer (1) Fengguang Song Department of Computer & Information Science IUPUI

Order Analysis of Algorithms. Sorting problem

Code generation and local optimization

EECS 583 Class 8 Classic Optimization

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

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

The View from 35,000 Feet

Lecture 1 Introduction. I. Why Study Compilers? II.Example: Machine Learning III. Course Syllabus

Data Flow Analysis. Program Analysis

Code Generation. M.B.Chandak Lecture notes on Language Processing

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

Appendix A The DL Language

Simone Campanoni Loop transformations

Lecture Notes on Value Propagation

Code Generation (#')! *+%,-,.)" !"#$%! &$' (#')! 20"3)% +"#3"0- /)$)"0%#"

Introduction to Machine-Independent Optimizations - 1

Transcription:

What Do Compilers Do? Lecture 1 Introduction I What would you get out of this course? II Structure of a Compiler III Optimization Example Reference: Muchnick 1.3-1.5 1. Translate one language into another e.g., convert C++ into SPARC object code difficult for natural languages, but feasible for computer languages 2. Improve (i.e. optimize ) the code e.g, make the code run 3 times faster driving force behind modern RISC microprocessors Optimizing Compilers: Introduction 1 T. Mowry Optimizing Compilers: Introduction 2 T. Mowry What Do We Mean By Optimization? Informal Definition: transform a computation to an equivalent but better form in what way is it equivalent? in what way is it better? Optimize isabitofamisnomer the result is not actually optimal Full Employment Theorem How Can the Compiler Improve Performance? Execution time = Operation count * Machine cycles per operation Minimize the number of operations arithmetic operations, memory acesses Replace expensive operations with simpler ones e.g., replace 4-cycle multiplication with 1-cycle shift Minimize cache misses both data and instruction accesses Processor cache memory Related issue: minimize object code size more important on special-purpose processors (e.g. DSPs) Optimizing Compilers: Introduction 3 T. Mowry Optimizing Compilers: Introduction 4 T. Mowry

Why Study Compilers? Crucial for anyone who cares about performance speed of system = hardware + compilers Key ingredient in modern processor architecture development Compilation: heart of computing maps a high-level abstract machine to a lower level one An example of a large software program Problem solving find common cases, formulate problem mathematically, develop algorithm, implement, evaluate on real data Software engineering build layers of abstraction (based on theory) and support with tools Silicon Compilers CAD tools increasingly rely on optimization optimizing a hardware design is similar to optimizing a program What Would You Get Out of This Course? Basic knowledge of existing compiler optimizations Hands-on experience in constructing optimizations within a fully functional research compiler Basic principles and theory for the development of new optimizations Understanding of the use of theory and abstraction to solve future problems Optimizing Compilers: Introduction 5 T. Mowry Optimizing Compilers: Introduction 6 T. Mowry II. Structure of a Compiler Source Code Intermediate Form Object Code C C++ Java Verilog Front End Optimizer Optimizations are performed on an intermediate form similar to a generic RISC instruction set Back End Alpha SPARC x86 IA-64 Ingredients in a Compiler Optimization Formulate optimization problem Identify opportunities of optimization applicable across many programs affect key parts of the program (loops/recursions) amenable to efficient enough algorithm Representation Must abstract essential details relevant to optimization Analysis Detect when it is legal and desirable to apply transformation Code Transformation Experimental Evaluation (and repeat process) Allows easy portability to multiple source languages, target machines Optimizing Compilers: Introduction 7 T. Mowry Optimizing Compilers: Introduction 8 T. Mowry

Three-address code A := B op C Representation: Instructions LHS: name of variable e.g. x, A[t] (address of A + contents of t) RHS: value Typical instructions A := B op C A := unaryop B A := B GOTO s IF A relop B GOTO s CALL f RETURN III. Optimization Example Bubblesort program that sorts an array A that is allocated in static storage: an element of A requires four bytes of a byte-addressed machine elements of A are numbered 1 through n (n is a variable) A[j] is in location &A+4*(j-1) FOR i := n-1 DOWNTO 1 DO FOR j := 1 TO i DO IF A[j]> A[j+1] THEN BEGIN temp := A[j]; A[j] := A[j+1]; A[j+1] := temp END Optimizing Compilers: Introduction 9 T. Mowry Optimizing Compilers: Introduction 10 T. Mowry Translated Code Representation: a Basic Block i := n-1 S5: if i<1 goto s1 j := 1 s4: if j>i goto s2 t1 := j-1 t4 := j+1 t5 := t4-1 t6 := 4*t5 if t3<=t7 goto s3 t8 :=j-1 t9 := 4*t8 temp := A[t9] ;A[j] t10 := j+1 t11:= t10-1 t12:= 4*t11 t13 := A[t12] ;A[j+1] t14 := j-1 t15 := 4*t14 A[t15] := t13 ;A[j]:=A[j+1] t16 := j+1 t17 := t16-1 t18 := 4*t17 A[t18]:=temp ;A[j+1]:=temp s3:j := j+1 goto S4 S2:i := i-1 goto s5 s1: Basic block = a sequence of 3-address statements only the first statement can be reached from outside the block (no branches into middle of block) all the statements are executed consecutively if the first one is (no branches out or halts except perhaps at end of block) We require basic blocks to be maximal they cannot be made larger without violating the conditions Optimizations within a basic block are local optimizations Optimizing Compilers: Introduction 11 T. Mowry Optimizing Compilers: Introduction 12 T. Mowry

Flow Graphs in Example Nodes: basic blocks B1 i:=n-1 Edges: B i -> B j,iffb j can follow B i immediately in some execution B2 if i<1 goto out out Either first instruction of B j is target of a goto at end of B i Or, B j physically follows B i, which does not end in an unconditional goto. B3 j:=1 B5 i := i-1 The block led by first statement of the program is the start, or entry node. B4 B6 if j>i goto B5 t1 := j-1... B7 B8 t8 :=j-1... A[t18]=temp j := j+1 Optimizing Compilers: Introduction 13 T. Mowry Optimizing Compilers: Introduction 14 T. Mowry Algorithm optimization Sources of Optimization Local Optimizations Analysis & transformation performed within a basic block No control flow information is considered Algebraic optimization A := B+0 => A := B Local optimizations within a basic block -- across instructions Global optimizations within a flow graph -- across basic blocks Examples of local optimizations: local common subexpression elimination analysis: same expression evaluated more than once in b. transformation: replace with single calculation local constant folding or elimination analysis: expression can be evaluated at compile time transformation: replace by constant, compile-time value Interprocedural analysis within a program -- across procedures (flow graphs) dead code elimination Optimizing Compilers: Introduction 15 T. Mowry Optimizing Compilers: Introduction 16 T. Mowry

Example (Intraprocedural) Global Optimizations B3: j := 1 B4: if j>i goto B5 B6: t1 := j-1 t6 := 4*j B7:t8 :=j-1 t9 := 4*t8 temp := A[t9] ;temp:=a[j] t12:= 4*j t13 := A[t12] ;A[j+1] A[t9]:= t13 ;A[j]:=A[j+1] t18 := 4*j A[t18]:=temp ;A[j+1]:=temp B8:j := j+1 B5:i := i-1 Global versions of local optimizations global common subexpression elimination global constant propagation dead code elimination Loop optimizations reduce code to be executed in each iteration code motion induction variable elimination Other control structures Code hoisting: eliminates copies of identical code on parallel paths in a flow graph to reduce code size. Optimizing Compilers: Introduction 17 T. Mowry Optimizing Compilers: Introduction 18 T. Mowry Global Common Subexpression Elimination Induction Variable Elimination B3: j := 1 B4: if j>i goto B5 B6: t1 := j-1 t6 := 4*j B7: t8 :=j-1 t9 := 4*t8 temp:= A[t9] ;temp:=a[j] t12:= 4*j t13 := A[t12] ;A[j+1] A[t9] := t13 A[t12]:=temp B8: j := j+1 B5: i := i-1 Intuitively Loop indices are induction variables (counting iterations) Linear functions of the loop indices are also induction variables (for accessing arrays) Analysis: detection of induction variable Optimizations strength reduction: replace multiplication by additions elimination of loop index -- replace termination by tests on other induction variables Optimizing Compilers: Introduction 19 T. Mowry Optimizing Compilers: Introduction 20 T. Mowry

Example (after cse) Example (after iv) B3: j := 1 B4: if j>i goto B5 B6: t1 := j-1 t6 := 4*j B7: A[t2] := t7 A[t6] := t3 B8: j := j+1 B5: i := i-1 B3: t2 := 0 t6 := 4 B4: t19 := 4*i if t6>t19 goto B5 B6: t3 := A[t2] B7: A[t2] := t7 A[t6] := t3 B8: t2 := t2+4 t6 := t6+4 B5: i := i-1 Optimizing Compilers: Introduction 21 T. Mowry Optimizing Compilers: Introduction 22 T. Mowry Loop Invariant Code Motion Analysis a computation is done within a loop and result of the computation is the same as long as we keep going around the loop Transformation move the computation outside the loop Machine Dependent Optimizations Register allocation Instruction scheduling Memory hierarchy optimizations etc. Optimizing Compilers: Introduction 23 T. Mowry Optimizing Compilers: Introduction 24 T. Mowry