1 CS 201 Compiler Construction Machine Code Generation.

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

CSE 5317/4305 L9: Instruction Selection1 Instruction Selection Leonidas Fegaras.
Tiling Examples for X86 ISA Slides Selected from Radu Ruginas CS412/413 Lecture on Instruction Selection at Cornell.
Target Code Generation
Intermediate Code Generation
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
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.
COMPILERS Register Allocation hussein suleman uct csc3005h 2006.
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
SSA.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
8 Intermediate code generation
COMPILERS Basic Blocks and Traces hussein suleman uct csc3005h 2006.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Code Generation Professor Yihjia Tsai Tamkang University.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Register Allocation (via graph coloring)
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Data Flow Analysis Compiler Design Nov. 8, 2005.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Compiler Chapter# 5 Intermediate code generation.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
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.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Intermediate Code Representations
Compilers Modern Compiler Design
Compiler Principles Fall Compiler Principles Lecture 8: Intermediate Representation Roman Manevich Ben-Gurion University.
1 March 16, March 16, 2016March 16, 2016March 16, 2016 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel: (800)
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
More Code Generation and Optimization Pat Morin COMP 3002.
Chapter 8 Code Generation
Code Optimization.
Optimization Code Optimization ©SoftMoore Consulting.
Instruction Scheduling Hal Perkins Summer 2004
Unit IV Code Generation
Chapter 6 Intermediate-Code Generation
Instruction Scheduling Hal Perkins Winter 2008
CS 201 Compiler Construction
CS 201 Compiler Construction
TARGET CODE GENERATION
Interval Partitioning of a Flow Graph
Instruction Scheduling Hal Perkins Autumn 2005
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Compiler Construction
Code Generation Part II
Target Code Generation
Instruction Scheduling Hal Perkins Autumn 2011
Code Optimization.
CS 201 Compiler Construction
CS 201 Compiler Construction
Presentation transcript:

1 CS 201 Compiler Construction Machine Code Generation

Code Generation – Naïve Approach Macro expand each intermediate code statement into a sequence of target machine instructions. 2 A=B+C M=A+B Load A, R1 ADD C, R1 Store R1, A Load A, R1 -- redundant ADD B, R1 Store R1, M

Naïve Approach Contd.. Assumption: All registers are free immediately preceding and following an intermediate code statement. + each intermediate code statement can be translated independently - code is of poor quality – too many loads and stores. To obtain good quality code multiple intermediate code statements should be examined together and register allocation and assignment should be performed to minimize loads and stores. 3

Register Allocation & Assignment Allocation: What variables should be put into registers? Assignment: What specific registers should be assigned at each program point? 4 Local Register Allocation: Examine each basic block independently, i.e. all register are free at basic block boundaries. Global Register Allocation: Examine the code for each function independently, i.e. all registers are free at function boundaries.

Register Allocation & Assignment 5 Generating code for A=B+C assuming B is in R1 1. ADD C,R1 – neither B or C available in register 2. Load C, R2 – C available in R2; B not available ADD R2, R1 3. Load C, R2 – C not available; B available in R1 ADD R1, R2 Usage of B and C after A=B+C determines the preferable choice.

Order of Statements Matters (A+B) – (C – X*Z) 6 1. T1 = A + B 2. T2 = X * Z 3. T3 = C - T2 4. T4 = T1 – T3 1. Load A, R1 1. Add B, R1 2. Load X, R2 2. Mult Z, R2 Store R1, T1 3. Load C, R1 3. Sub R2, R1 Load T1, R2 4. Sub R1, R2 4. Store R2, T4 1. T2 = X * Z 2. T3 = C - T2 3. T1 = A + B 4. T4 = T1 – T3 1. Load X, R1 1. Mult Z, R1 2. Load C, R2 2. Sub R1, R2 3. Load A, R1 3. Add B, R1 4. Sub R2, R1 4. Store R1, T4 Spill Code

Order of Statements Contd… The ordering of statements effects maximum number of simultaneously live variables and hence the minimum number of registers needed to avoid spill code. Rest of the discussion: + Code reordering within basic blocks + Global register allocation 7

Reordering Code Statements DAG – Directed Acyclic Graph A representation to assist in code reordering. + Nodes are operations + Edges represent dependences Nodes are labeled as follows: 1. Leaves with variables or constants – subscript 0 is used to distinguish initial value of the variable from other values. 2. Interior nodes with operators and list of variables whose values are computed by the node. 8

DAGs Contd.. DAGs are useful for: 1. Removing common local sub-expressions. 2. Renaming temporaries. 3. Finding names used inside the block but evaluated outside. 4. Finding statements in the block that could have their computed values used outside the block. 5. Statements that can be reordered (or executed in parallel). 9

DAG Construction Given a function node(identifier) – it returns the node currently associated with the identifier. Process one statement at a time as follows: Statement types: x = y op z; x = op y; x = y; 1. If node(y) is undefined then create a leaf labeled y o and this is now node(y) in case of x = y op z; do the same for z. 2. Determine if there is a node labeled “op” with node(y) & node(z) as the left and right children in case of x = y op z. 10

DAG Construction Contd.. 2 Contd. In case of x = op y, check if there is a node labeled “op” with single child node(y). If not, create such a node. Let the node found or created by n. 3.Delete x from the list of identifiers attached to node(x) & append x to the list of identifiers attached to node n. Set node(x) to n. 11

Example of DAG Construction 12 T1 = J * 2 T2 = X[T1] T3 = T2 + N T4 = J * 2 T5 = Y[T4] T6 = T5 + T3 ANS = T6 T7 = J+ 1 J = T7 ANS = J If J<10 goto L

Code Reordering DAG captures all legal re-orderings. Which ordering should we select? Try to place computation & use of a value next to each other so that the register used by the value is freed immediately. Next we will look at a heuristic that attempts, as far as possible, to make the evaluation of a node immediately follow the evaluation of the leftmost argument. 13

Code Reordering Contd.. Algorithm: List nodes in reverse order. While unlisted interior nodes remain Do select an unlisted node n, all of whose parents have been listed list n While leftmost child m of n has no unlisted parents and is not a leaf Do list m n = m End while 14

Example 15

Sample Problem 16