Introduction to Advanced Topics Chapter 1 Text Book

Introduction to Advanced Topics Chapter 1 Text Book: Advanced compiler Design implementation By Steven S Muchnick (Elsevier)

Outline • Review of compiler structure • Advanced issues in elementary topics • The importance of optimizations • Structure of optimizing compilers • Placement of optimizations • Advanced topics

Compiler Structure String of characters Scanner tokens Symbol table and access routines Parser AST Semantic analyzer IR Code Generator Object code OS Interface

Advanced Issues in Elementary Topics • Symbol table management(3) – Efficiency – Overloading • • Type Inference Intermediate Language Selection(4) Run-Time Support(5) Producing Code Generators (6)

Intermediate Language Selection • • • Low Vs. High level control flow structures Flat Vs. Hierarchical (tree) Machine Vs. High level of instructions (Symbolic) Registers Vs. Stack Normal forms (SSA) Intermediate forms: Control Flow Graph, Call Graph, Program Dependence Graph • Issues: Engineering, efficiency, portability, optimization level

IRs in the Book HIR MIR s 2 s 1 v v 1 for v v 1 by v 2 to v 3 do s 4 s 3 t 2 v 2 a[i] : =2 endfor LIR s 6 s 5 t 3 v 3 L 1: if v >t 3 goto L 2 L 1: if s 2 >s 6 goto L 2 s 7 addr a t 4 addr a s 8 4*s 9 t 5 4*i s 10 s 7+s 8 t 6 t 4+t 5 [s 10] 2 *t 6 2 s 2 + s 4 v v + t 2 goto L 1 L 2:

Advanced Issues in Elementary Topics • Symbol table management(3) – Efficiency – Overloading • • Type Inference Intermediate Language Selection(4) Run-Time Support(5) Producing Code Generators (6)

Run-Time Support • • • Data representation and instructions Register usage Stack frames (activation records) Parameter passing disciplines Symbolic language support Garbage Collection

Advanced Issues in Elementary Topics • Symbol table management(3) – Efficiency – Overloading • • Type Inference Intermediate Language Selection(4) Run-Time Support(5) Producing Code Generators (6)

The Importance of Optimizations • One pass compilers produce slow code • Much of the time spent in loops – optimize loops • Machines can be simpler and faster if optimizing compilers are used (RISC, VLIW) • Programs are complex and general • Compilers need to be modular

SPARC code int a, b, c, d; c = a + b; d = c + 1; ldw a, r 1 Optimized ldw b, r 2 SPARC code add r 1, r 2, r 3 stw r 3, c add ldw c, r 3 add r 3, 1, r 4 stw r 4, d 10 cycles r 1, r 2, r 3 2 cycles

Application Dependent Optimizations • Functional programs – replacement of recursion by loops (tail-calls elimination) – replacement of heap by stack • Object Oriented Programs – Dead member elimination – Replacement of virtual by static function • Numeric Code • Database Code

String Scanner Tokens Parser AST Semantic AST LIR generator LIR Optimizer LIR Fin. assembly Object String Scanner Tokens Parser AST Semantic AST IR generator MIR Optimizer MIR Code generator LIR Post. optimizer Object

Mixed vs. Low Level Optimizers • Mixed – Sun-SPARC, Dec. Alpha, SGI-MIPS, Intel’s 386 J Easier to port J More efficient compilation? J Supports CISC • Low Level • HP-PA-RISC/IBM-Power PC J More efficient code J Conceptually simpler (in RISC) • Used for multiple programming languages

Translation by Preprocessing • Some programming languages are translated into C – Elimination of includes, C++(cfront), Haskel • Quick and portable solution • C supports some indirect source level debugging • Other examples: Fortran into “vector” Fortran program

Data-Cache Optimizations(20) String Scanner Tokens Parser AST Semantic HIR Data-cache optimizer MIR

IBM Power. PC compiler String Scanner Tokens Parser AST Semantic AST LIR generator LIR=XIL Optimizer LIR Fin. assembly Object Low-to-High HIR=YIL Data-cache optimizer HIR=YIL High-to-Low

Outline • Review of compiler structure • Advanced issues in elementary topics • The importance of optimizations • Structure of optimizing compilers • Placement of optimizations • Advanced Topics

Scalar replacement of array references Data-cache optimizations Inline expansion Leaf-routine optimizations Instruction Scheduling 1 Register allocation Instruction Scheduling 2 Intraprocedural I-cache optimizations Instruction prefetching Data prefertching Branch predication Procedure integration Tail-call elimination Scalar replacement of aggregates Sparse constant propagation Interprocedural constant propagation Procedure specialization and cloning Sparse conditional constant propagation Global value numbering Local and global copy propagation Sparse conditional constant propagation Dead code elimination Common Subexpression Elimination Interprocedural register allocation Loop invariant code motion Interprocedural I-cache optimization Partial redundency Elimination
- Slides: 19