optimizing compilers cisc 673 spring 2009 overview of compilers and jikesrvm
Post on 01-Feb-2016
78 Views
Preview:
DESCRIPTION
TRANSCRIPT
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
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
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)
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
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
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
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
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!
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
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?
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
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?
UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 13
Change in Execution Time
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)
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
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)
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)
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
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
UUNIVERSITY OF NIVERSITY OF DDELAWARE • ELAWARE • Computer & Information Sciences Department Computer & Information Sciences Department 20
A Tour of the JikesRVM codebase
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
top related