Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

21
UNIVERSITY NIVERSITY OF OF D DELAWARE ELAWARE • C • COMPUTER OMPUTER & I & INFORMATION NFORMATION S SCIENCES CIENCES D DEPARTMENT EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM John Cavazos University of Delaware

description

Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM. John Cavazos University of Delaware. Compiler Overview. Source program. Lexical analyzer. Syntax analyzer. Semantic analyzer. Symbol-table. Error handler. Code optimizer. Code generator. Target program. - PowerPoint PPT Presentation

Transcript of Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

Page 1: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITYNIVERSITY OFOF D DELAWAREELAWARE • C• COMPUTEROMPUTER & I & INFORMATIONNFORMATION S SCIENCESCIENCES D DEPARTMENTEPARTMENT

Optimizing CompilersCISC 673

Spring 2009Overview of Compilers and JikesRVM

John CavazosUniversity of Delaware

Page 2: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 2

Compiler Overview

Lexical analyzer

Syntax analyzer

Semantic analyzer

Code optimizer

Code generator

Source program

Target program

Error handlerSymbol-table

Page 3: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 3

Compiler Frontend

Lexical Analyze

rParser

Semantic

Analyzer

Source Progra

m

Token Stream

Syntax Tree

Syntax Tree

Intermediate Rep Gen

IR

• Series of passes

• Source program – Written in a HLL

• Lexical analysis – Convert keywords into “tokens”

• Parser – Forms a syntax “tree” (statements, expressions, etc.)

• Semantic analysis – Type checking, etc.

• We will not cover the front end in this class! (See CISC 672)

Page 4: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 4

Compiler Middleend and Backend

Code Optimize

r

Code Generator

IR IR Target program

• This is where the “interesting” stuff happens … enough to fill an entire grad-level course

• Code optimization – “improves” the intermediate code Consists of machine independent &

dependent opts

• Code generation – register allocation, instruction scheduling

Page 5: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 5

Traditional Optimizations

• Analyze the program Where is this value used? Is this value recomputed?

• Reduce the total number of operations Common subexpression elimination Strength reduction

• Maintain values in registers Elimination of redundant loads

Page 6: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 6

Developing an Optimization

Formulate Implement Evaluate

Refine

• Formulate the problem based on: Intuition Extract properties from a program

• Implement the algorithm

• Evaluate results based on test programs

• Use results to refine the algorithm

Page 7: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 7

Building an Optimizing Compiler

Strict requirements

• Must be correct for all possible inputs

• Must provide robust solution Small changes in input should not produce

wild changes in output

Good optimizing compilers are crafted

• Careful selection of transformations

• Careful use of algorithms and data structures

Page 8: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 8

Building an Optimizing Compiler

Compilers are engineered objects

• Try to minimize running time of compiled code

• Try to minimize compile time

• Try to limit use of compile-time space

• Try to keep engineering efforts reasonable

With all these constraints, results are …

unexpected!

Page 9: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 9

Quick Look at Real Compilers

Consider inline substitution

• Replace procedure call with body of called procedure Rename to handle naming issues Widely used (and important!) for optimizing

OOPs

Page 10: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 10

Characteristics of Function Inlining

• Safety: almost always safe

• Profitability: avoid overhead of a procedure call

• Opportunity: inline leaf procedures

How well do compilers handle inlined code?

Page 11: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 11

5 Real Good Compilers

SourceProgram Compiler

Inliner Compiler Execute & time

Experimental Setup

Execute & time

Page 12: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 12

The Study

Cooper/Hall/Torczon (Software-Practice & Experience 91)

• Eight programs, five compilers, five processors

• Eliminated over 99% of dynamic calls in 5 of programs

• Measured speed of original versus transformed code

• Expected uniform speed up, at least from call overhead

• What really happened?

Page 13: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 13

Change in Execution Time

Page 14: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 14

Happens with Good Compilers!

• Input code violated assumptions made by compiler writers Longer procedures More names Different code shapes

• Exacerbated problems that are hard to detect! Imprecise analysis Algorithms that scale poorly Tradeoffs between global and local speed Limitations in the implementations

The compiler writers were surprised (most of them)

Page 15: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 15

JikesRVM

Translation From Bytecode to HIR

Optimization of HIRHIR Jikes Front

End

Translation From HIR to LIR

Optimization of LIR

Translation From LIR To MIR

Optimization of MIR

Final Assembly

Jikes BackEnd

LIR

MIR

Optimized HIR

Optimized LIR

Optimized MIR

Binary Code

Page 16: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 16

Levels of IR

• HIR (High Level IR)

• LIR (Low Level IR)

• MIR (Machine Specific IR)

Page 17: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 17

HIR

• Operators similar to Java bytecode

• Example: ARRAYLENGTH, NEW, GETFIELD, BOUNDS_CHECK, NULL_CHECK

• Symbolic registers instead of an implicit stack

• Contains separate operators to implement explicit checks for run-time exceptions (eg., array-bounds checks)

Page 18: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 18

LIR

• Details of JikesRVM runtime and object layout

• Example: GET_TIB (vtable), INT_LOAD (for getfield)

• Expands complicated HIR structures such as TABLE_SWITCH

Page 19: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 19

MIR

• Similar to assembly code

• Details of target architecture are introduced

• Register Allocation is performed on MIR

Page 20: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 20

A Tour of the JikesRVM codebase

Page 21: Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM

UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 21

Next Time

Read the following Wikipedia pages (section) Graph Theory (Basics) Basic Blocks Control Flow Graphs