210 likes | 235 Views
Optimizing Compilers CISC 673 Spring 2011 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.
E N D
Optimizing CompilersCISC 673Spring 2011Overview 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
Intermediate Rep Gen IR Compiler Frontend • 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) Source Program Lexical Analyzer Token Stream Parser Syntax Tree Semantic Analyzer Syntax Tree
Compiler Middleend and Backend • 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 IR Code Optimizer IR Code Generator Target program
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
Developing an Optimization • 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 Formulate Implement Evaluate Refine
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
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!
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
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?
Inliner Compiler Execute & time Source Program Compiler Execute & time Experimental Setup 5 Real Good Compilers
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?
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)
JikesRVM Translation From Bytecode to HIR HIR Jikes Front End Optimization of HIR Optimized HIR Translation From HIR to LIR LIR Optimization of LIR Optimized LIR Jikes Back End Translation From LIR To MIR MIR Optimization of MIR Optimized MIR Final Assembly Binary Code
Levels of IR • HIR (High Level IR) • LIR (Low Level IR) • MIR (Machine Specific IR)
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)
LIR • Details of JikesRVM runtime and object layout • Example: GET_TIB (vtable), INT_LOAD (for getfield) • Expands complicated HIR structures such as TABLE_SWITCH
MIR • Similar to assembly code • Details of target architecture are introduced • Register Allocation is performed on MIR
Next Time • Read the following Wikipedia pages (section) • Graph Theory (Basics) • Basic Blocks • Control Flow Graphs