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.

Slides:



Advertisements
Similar presentations
1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization.
Advertisements

Register Allocation COS 320 David Walker (with thanks to Andrew Myers for many of these slides)
Register Allocation Consists of two parts: Goal : minimize spills
Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Register Allocation CS 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
Idea of Register Allocation x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy}
Coalescing Register Allocation CS153: Compilers Greg Morrisett.
1 CS 201 Compiler Construction Machine Code Generation.
A simple register allocation optimization scheme.
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.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
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-
Systems Architecture Lecture 5: MIPS Instruction Set
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
Carnegie Mellon 1 Optimal Scheduling “in a lifetime” for the SPIRAL compiler Frédéric de Mesmay Theodoros Strigkos based on Y. Voronenko’s idea.
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.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Liveness Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html://
1 Register Allocation Consists of two parts: –register allocation What will be stored in registers –Only unambiguous values –register assignment Which.
Code Generation Simple Register Allocation Mooly Sagiv html:// Chapter
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
Instruction Selection, II Tree-pattern matching Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in.
Code Generation Mooly Sagiv html:// Chapter 4.
Improving Code Generation Honors Compilers April 16 th 2002.
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Code Generation Mooly Sagiv html:// Chapter 4.
Advanced Topics in Algorithms and Data Structures 1 An example.
1 October 18, October 18, 2015October 18, 2015October 18, 2015 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel:
Chapter 7 Syntax-Directed Compilation (AST & Target Code) 1.
Intermediate Code Representations
COMPILERS Instruction Selection hussein suleman uct csc305h 2005.
Compilers Modern Compiler Design
Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.
Register Allocation CS 471 November 12, CS 471 – Fall 2007 Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
COMPILERS Instruction Selection hussein suleman uct csc3003s 2007.
Code Generation How to produce intermediate or target code.
Compiler Principles Fall Compiler Principles Lecture 8: Intermediate Representation Roman Manevich Ben-Gurion University.
Instruction Scheduling Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
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.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Chapter 8 Code Generation
COMPILERS Instruction Selection
Local Register Allocation & Lab Exercise 1
Optimization Code Optimization ©SoftMoore Consulting.
Instruction Scheduling Hal Perkins Summer 2004
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
Systems Architecture Lecture 5: MIPS Instruction Set
Instruction Scheduling Hal Perkins Winter 2008
CS 201 Compiler Construction
Instruction Selection, II Tree-pattern matching
Local Register Allocation & Lab Exercise 1
Instruction Scheduling Hal Perkins Autumn 2005
Local Optimizations.
8 Code Generation Topics A simple code generator algorithm
Compiler Construction
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
Instruction Scheduling Hal Perkins Autumn 2011
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

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 r1, c, r2// r2 := *(r1+c) loadAO r1, r2, r3 i2i r1, r2// r2 := r1 (for integers) cmp_LE r1, r2, r3// if r1<=r2, r3:=true, else r3:=false cbr r1, L1, L2// if (r1) goto L1, else goto L2 jump r1 represents x's offset from the sp

2 Code generation Let's start with some examples. Generate code from a tree representing x = a+2 - (c+d-4) Issues: which children should go first? what if we already had a-c in a register? Does it make a difference if a and c are floating point as opposed to integer? Generate code for a switch statement Generate code for w = w*2*x*y*zw = w*2*x*y*z

3 Code generation Code generation = instruction selection instruction scheduling register allocation

4 Instruction selection IR to assembly Why is it an issue? Example: copy a value from r1 to r2 Let me count the ways... Criteria How hard is it? Use a cost model to choose. How about register usage?

5 Instruction selection How hard is it? Can make locally optimal choices Global optimality is NP-complete Criteria speed of generated code size of generated code power consumption Considering registers Assume enough registers are available, let register allocator figure it out.

6 Instruction scheduling Reorder instructions to hide latencies. Example: ( ) r1 ( ) addr1, r1, r1 ( ) r2 ( ) multr1, r2, r1 ( ) r2 ( ) mult r1, r2, r1 ( ) loadAI r2 ( ) multr1, r2, r1 ( ) storeAIr1, memory ops : 3 cycles multiply : 2 cycles everything else: 1 cycle

7 Instruction scheduling Reorder instructions to hide latencies. Example: (1) r1 (4) addr1, r1, r1 (5) r2 (8) multr1, r2, r1 (9) r2 (12) mult r1, r2, r1 (13) loadAI r2 (16) multr1, r2, r1 (18)storeAIr1, (20) (1) r1 (2) r2 (3) r3 (4) addr1, r1, r1 (5) multr1, r2, r1 (6) loadAI r2 (7) mult r1, r3, r1 (9) multr1, r2, r1 (11) storeAIr1, (13)

8 Instruction scheduling Reorder instructions to hide latencies. Example2: (1) r1 (4) multr1, r1, r1 (6) mult r1, r1, r1 (8) multr1, r1, r1 (10) storeAIr1,

9 Instruction scheduling Reorder instructions to hide latencies. We need to collect dependence info Scheduling affects register lifetimes ==> different demand for registers Should we do register allocation before or after? How hard is it? more than one instructions may be ready too many variables may be live at the same time NP-complete!

10 Register allocation Consists of two parts: register allocation register assignment Goal : minimize spills How hard is it? BB w/ one size of data: polynomial otherwise, NP-complete based on graph coloring.

11 Code generation Generating code for simple expressions If the expression is represented by a tree, a post-order walk gives an evaluation sequence Changing the evaluation sequence may result in code that uses fewer registers. Idea: find out how many registers are needed for each subtree and evaluate the most expensive one first. Consider the following algorithm that labels tree nodes with the number of registers needed (for the ILOC architecture): 1 if n is a left leaf, or right leaf and variable 0 if n is a right leaf and constant max(label lchild, label rchild ) if label lchild  label rchild label lchild +1 if label lchild == label rchild label(n) =

12 Code generation Generating code for simple expressions The idea behind our algorithm: Variables need to be loaded in registers, so leaves need one register For rhs constants we can use opI operations, so no extra register needed If we need k registers for each subtree, we'll use k for the left one, keep 1 for the result, and then use another k for the right subtree. That's a total of k+1 registers If we need k registers for one subtree and m<k for the other, then use k for the one subtree, keep the result in 1, use the remaining k-1 (which is at least m) for the other subtree. That's a total of k registers. When generating code, make sure to evaluate the most expensive subtree first.

13 Code generation Generating code for expressions What if an expression contains a function call? Should the compiler be allowed to change the evaluation order? Big idea #1 wrt to optimizing or moving code around : Be conservative! Generating code for boolean operators relational operators read the book array references