A simple register allocation optimization scheme.

Slides:



Advertisements
Similar presentations
Register Allocation COS 320 David Walker (with thanks to Andrew Myers for many of these slides)
Advertisements

Calling sequence ESP.
Target Code Generation
Register Allocation CS 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
1 Code generation Our book's target machine (appendix A): opcode source1, source2, destination add r1, r2, r3 addI r1, c, r2 loadI c, r2 load r1, r2 loadAI.
Coalescing Register Allocation CS153: Compilers Greg Morrisett.
Compiler Construction Sohail Aslam Lecture ExampleExample a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2.
1 CS 201 Compiler Construction Machine Code Generation.
Intermediate Representation III. 2 PAs PA2 deadline is
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
COMP 2003: Assembly Language and Digital Logic
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Allocating Memory.
Lecture 26 Epilogue: Or Everything else you Wanted to Know about Compilers (more accurately Everything else I wanted you to Know) Topics Getreg – Error.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Register Allocation (Slides from Andrew Myers). Main idea Want to replace temporary variables with some fixed set of registers First: need to know which.
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Memory Management - 2 CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
CS 536 Spring Code generation I Lecture 20.
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Lecture 25 Generating Code for Basic Blocks Topics Code Generation Readings: April 19, 2006 CSCE 531 Compiler Construction.
Improving Code Generation Honors Compilers April 16 th 2002.
Register Tracking Register tracking improves on a simple code generator Uses a simple local register allocation scheme in which the contents of allocatable.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Basics of Operating Systems March 4, 2001 Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard.
Linked Lists in MIPS Let’s see how singly linked lists are implemented in MIPS on MP2, we have a special type of doubly linked list Each node consists.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Activation Records (in Tiger) CS 471 October 24, 2007.
8.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Paging Physical address space of a process can be noncontiguous Avoids.
Simple Code Generation CS153: Compilers. Code Generation Next PS: Map Fish code to MIPS code Issues: –eliminating compound expressions –eliminating variables.
Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map.
Compiler Construction Code Generation Activation Records
Compiler Principles Fall Compiler Principles Lecture 12: Register Allocation Roman Manevich Ben-Gurion University.
Overview of Back-end for CComp Zhaopeng Li Software Security Lab. June 8, 2009.
Compilation (Semester A, 2013/14)
Reading Condition Codes (Cont.)
Credits and Disclaimers
Module 11: File Structure
Assembly Lab 3.
Fall Compiler Principles Lecture 9: Register Allocation
Chapter 11 Instruction Sets
Homework In-line Assembly Code Machine Language
Introduction to Compilers Tim Teitelbaum
Assembly Language Programming V: In-line Assembly Code
6.001 SICP Stacks and recursion
Machine-Level Programming 4 Procedures
Instruction Scheduling for Instruction-Level Parallelism
Programming Languages (CS 550) Mini Language Compiler
Condition Codes Single Bit Registers
Unit IV Code Generation
CS 201 Compiler Construction
MIPS Procedure Calls CSE 378 – Section 3.
Machine-Level Representation of Programs III
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
System Calls David Ferry CSCI 3500 – Operating Systems
6.001 SICP Explicit-control evaluator
Lecture 16: Register Allocation
TARGET CODE -Next Usage
8 Code Generation Topics A simple code generator algorithm
Compiler Construction
Code Generation Part II
Target Code Generation
Credits and Disclaimers
Programming Languages (CS 360) Mini Language Compiler
Computer Architecture and System Programming Laboratory
Presentation transcript:

A simple register allocation optimization scheme

2 Register allocation example x=y+z Move z,R1 Add y,R1 Move R1,x mov -12(%ebp),%eax mov %eax,-16(%ebp) mov -8(%ebp),%eax add %eax,-16(%ebp) mov -16(%ebp),%eax mov %eax,-4(%ebp) mov -12(%ebp),%eax # now z and R1 is in eax mov -8(%ebp),%ebx # now y is in ebx add %ebx,%eax mov %eax,-4(%ebp) Naïve translation Optimized translation 6 memory accesses3 memory accesses file.ic file.lir

3 Optimizing register allocation Goal: associate machine registers with LIR registers as much as possible Optimization done per IC statement (sequence of LIR instructions translated from same statement) Basic idea: store first 6 LIR registers in machine registers and the rest in frame But decide on which ones during translation Track association between LIR registers and machine registers using RegMap Use values stored in RegMap to translate LIR instructions Flush register values back to frame if needed

4 RegMap Main data structure is RegMap – a map from x86 registers to Free/Reg: Free – x86 register is currently available Reg – LIR register U – used by some variable Operations supported by RegMap: RegMap.get : x86Reg -> LIR register or Free RegMap.get : LIR register -> x86Reg or null RegMap.put(x86Reg,LIRReg) – create an association RegMap.getFreeReg – returns an available x86 registers if there is any, otherwise flush a register back to frame and return an available register E.g., if edx is associated with R3 with offset -12 then emit mov %edx,-12(%ebp) and return edx

5 Definitions For each LIR instruction, define: Read registers – LIR registers storing values used by the instruction Write registers – LIR registers storing result of instruction Example: Add op1,op2 Read registers = {op1,op2}  LIRRegs Write registers = {op2}  LIRRegs op1 can be LIRReg/Immediate/Memory op2 can be LIRReg/Memory

6 GetOp(op) : immediate or x86 register if op is Immediate return immediate else if op is Memory with offset op_offset xreg = RegMap.getFreeReg() emit mov op_offset(%ebp),xreg return xreg else if op is LIRReg with offset op_offset if (xreg,op) in RegMap return xreg else xreg = RegMap.getFreeReg() emit mov op_offset(%ebp),xreg RegMap.put(xreg,op) Translate Add op1,op2 val1 = GetOp(op1) val2 = GetOp(op2) emit add va1l,val2 if op2 is Memory emit mov val2,op2_offset(%ebp) Translating Add op1,op2

7 Translation examples AXBXCXDXSIDI FFFFFF Move z,R1 AXBXCXDXSIDI R1FFFFF AXBXCXDXSIDI R1R2R3R4R5R6 Move z,R7 AXBXCXDXSIDI R7R2R3R4R5R6 mov %eax,-12(%ebp) #R1 Store R1 back in frame mov 8(%ebp),%eax

8 Translation examples AXBXCXDXSIDI R1R2R3R4R5R6 Move y,R1 AXBXCXDXSIDI R1R2R3R4R5R6 Add R2,R1 AXBXCXDXSIDI R1R2R3R4R5R6 is this entry going to be used again? mov 12(%ebp),%eax add %ebx,%eax

9 Translation algorithm Identify sequences of LIR instructions that correspond to same IC statement Initialize RegMap to be empty Translate each LIR instruction using RegMap If LIR register Rx is read and not write can clear its entry Special cases: LIR registers used for conditions in if/while statements – used in Compare instructions When flushing register to frame how do we choose which one? Decide on policy, e.g., least frequently used

10 Example revisited Move z,R1 Add y,R1 Move R1,x # Move z,R1 map={} mov -12(%ebp),%eax # map={eax=R1} # Add y,R1 mov -8(%ebp),%ebx # use free register ebx for y # map={eax=R1,ebx=U} add %ebx,%eax # map={eax=R1} # Move R1,x mov %eax,-4(%ebp) R1 read and not write – clear entry map={} Optimized translation file.lir x=y+z file.ic

11 Conclusion Very naïve scheme for register allocation during code generation Works better if number of LIR registers small (PA4 optimizations) Can generalize to basic blocks – sequence of LIR instructions without labels and jumps Really just a hack Better technique – liveness analysis with graph coloring Minimizes number of registers for entire function (not just single statement) Can allocate same register for different local variables

12 That’s all folks