Global Register Allocation

Size: px
Start display at page:

Download "Global Register Allocation"

Transcription

1 Global Register Allocation

2 Lecture Outline Memory Hierarchy Management Register Allocation via Graph Coloring Register interference graph Graph coloring heuristics Spilling Cache Management 2

3 The Memory Hierarchy Registers 1 cycle bytes Cache 3 cycles 256k-16M Main memory cycles 512M-64G Disk 0.5-5M cycles 10G-1T 3

4 Managing the Memory Hierarchy Programs are written as if there are only two kinds of memory: main memory and disk Programmer is responsible for moving data from disk to memory (e.g., file I/O) Hardware is responsible for moving data between memory and caches Compiler is responsible for moving data between memory and registers 4

5 Current Trends Power usage limits Size and speed of registers/caches Speed of processors Improves faster than memory speed (and disk speed) The cost of a cache miss is growing The widening gap between processors and memory is bridged with more levels of caches It is very important to: Manage registers properly Manage caches properly Compilers are good at managing registers 5

6 The Register Allocation Problem Recall that intermediate code uses as many temporaries as necessary This complicates final translation to assembly But simplifies code generation and optimization Typical intermediate code uses too many temporaries The register allocation problem: Rewrite the intermediate code to use at most as many temporaries as there are machine registers Method: Assign multiple temporaries to a register But without changing the program behavior 6

7 History Register allocation is as old as intermediate code Register allocation was used in the original FORTRAN compiler in the 50s Very crude algorithms were used back then A breakthrough was not achieved until 1980 Register allocation scheme based on graph coloring Relatively simple, global, and works well in practice 7

8 An Example Consider the program a := c + d e := a + b f := e - 1 with the assumption that a and e die after use Temporary a can be reused after a + b Same with temporary e after e - 1 Can allocate a, e, and f all to one register (r 1 ): r 1 := r 2 + r 3 r 1 := r 1 + r 4 r 1 := r 1-1 8

9 Basic Register Allocation Idea The value in a dead temporary is not needed for the rest of the computation A dead temporary can be reused Basic rule: Temporaries t 1 and t 2 can share the same register if at all points in the program at most one of t 1 or t 2 is live! 9

10 Algorithm: Part I Compute live variables for each program point: {a,c,f} {c,d,f} {c,e} f := 2 * e {c,f} {b} a := b + c d := -a e := d + f {c,f} b := f + c b := d + e e := e - 1 {b,c,f} {c,d,e,f} {b,c,e,f} {b,e} 10

11 The Register Interference Graph Two temporaries that are live simultaneously cannot be allocated in the same register We construct an undirected graph with A node for each temporary An edge between t 1 and t 2 if they are live simultaneously at some point in the program This is the register interference graph (RIG) Two temporaries can be allocated to the same register if there is no edge connecting them 11

12 Register Interference Graph: Example For our example: f a b e c d E.g., b and c cannot be in the same register E.g., b and d can be in the same register 12

13 Register Interference Graph: Properties It extracts exactly the information needed to characterize legal register assignments It gives a global (i.e., over the entire flow graph) picture of the register requirements After RIG construction, the register allocation algorithm is architecture independent 13

14 Graph Coloring: Definitions A coloring of a graph is an assignment of colors to nodes, such that nodes connected by an edge have different colors A graph is k-colorable if it has a coloring with k colors 14

15 Register Allocation Through Graph Coloring In our problem, colors = registers We need to assign colors (registers) to graph nodes (temporaries) Let k = number of machine registers If the RIG is k-colorable then there is a register assignment that uses no more than k registers 15

16 Graph Coloring: Example Consider the example RIG a r 2 r 1 f b r 3 r 2 e c r 4 d r 3 There is no coloring with less than 4 colors There are various 4-colorings of this graph (one of them is shown in the figure) 16

17 Graph Coloring: Example Under this coloring the code becomes: r 2 := r 3 + r 4 r 3 := -r 2 r 2 := r 3 + r 1 r 1 := 2 * r 2 r 3 := r 3 + r 2 r 2 := r 2-1 r 3 := r 1 + r 4 17

18 Computing Graph Colorings The remaining problem is how to compute a coloring for the interference graph But: (1) Computationally this problem is NP-hard: No efficient algorithms are known (2) A coloring might not exist for a given number of registers The solution to (1) is to use heuristics We will consider the other problem later 18

19 Graph Coloring Heuristic Observation: Pick a node t with fewer than k neighbors in RIG Eliminate t and its edges from RIG If the resulting graph has a k-coloring then so does the original graph Why: Let c 1,,c n be the colors assigned to the neighbors of t in the reduced graph Since n < k we can pick some color for t that is different from those of its neighbors 19

20 Graph Coloring Simplification Heuristic The following works well in practice: Pick a node t with fewer than k neighbors Put t on a stack and remove it from the RIG Repeat until the graph has one node Then start assigning colors to nodes on the stack (starting with the last node added) At each step pick a color different from those assigned to already colored neighbors 20

21 Graph Coloring Example (1) Start with the RIG and with k = 4: f e Remove a a d b c Stack: {} 21

22 Graph Coloring Example (2) Start with the RIG and with k = 4: f e b c Stack: {a} Remove d d 22

23 Graph Coloring Example (3) Now all nodes have fewer than 4 neighbors and can be removed: c, b, e, f f e b c Stack: {d, a} 23

24 Graph Coloring Example (4) Start assigning colors to: f, e, b, c, d, a r 1 f a r 2 b r 3 r 2 e c r 4 d r 3 24

25 What if the Heuristic Fails? What if during simplification we get to a state where all nodes have k or more neighbors? Example: try to find a 3-coloring of the RIG: f a b e d c 25

26 What if the Heuristic Fails? Remove a and get stuck (as shown below) Pick a node as a possible candidate for spilling A spilled temporary lives is memory Assume that f is picked as a candidate f b e c d 26

27 What if the Heuristic Fails? Remove f and continue the simplification Simplification now succeeds: b, d, e, c b e c d 27

28 What if the Heuristic Fails? On the assignment phase we get to the point when we have to assign a color to f We hope that among the 4 neighbors of f we used less than 3 colors optimistic coloring? f b r 3 r 2 e c r 1 d r3 28

29 Spilling Since optimistic coloring failed, we must spill temporary f (actual spill) We must allocate a memory location as the home of f Typically this is in the current stack frame Call this address fa Before each operation that uses f, insert f := load fa After each operation that defines f, insert store f, fa 29

30 Spilling: Example This is the new code after spilling f a := b + c d := -a f := load fa e := d + f f := 2 * e store f, fa b := d + e e := e - 1 f := load fa b := f + c 30

31 Recomputing Liveness Information The new liveness information after spilling: {c,d,f} {c,f} {c,f} {a,c,f} {c,d,f} {c,e} f := 2 * e store f, fa {c,f} {b} a := b + c d := -a f := load fa e := d + f {c,f} f := load fa b := f + c b := d + e e := e - 1 {b,c,f} {c,d,e,f} {b,c,e,f} {b,e} 31

32 Recomputing Liveness Information New liveness information is almost as before f is live only Between a f := load fa and the next instruction Between a store f, fa and the preceding instruction Spilling reduces the live range of f And thus reduces its interferences Which results in fewer RIG neighbors for f 32

33 Recompute RIG After Spilling The only changes are in removing some of the edges of the spilled node In our case f now interferes only with c and d And now the resulting RIG is 3-colorable r2 a r2 f b r3 r2 e c r1 d r3 33

34 Spilling Notes Additional spills might be required before a coloring is found The tricky part is deciding what to spill Possible heuristics: Spill temporaries with most conflicts Spill temporaries with few definitions and uses Avoid spilling in inner loops Any heuristic is correct 34

35 Precolored Nodes Precolored nodes are nodes which are a priori bound to actual machine registers These nodes are usually used for some specific (time-critical) purpose, e.g.: for the frame pointer for the first N arguments (N=2,3,4,5) 35

36 Precolored Nodes (Cont.) For each color, there should be only one precolored node with that color; all precolored nodes usually interfere with each other We can give an ordinary temporary the same color as a precolored node as long as it does not interfere with it However, we cannot simplify or spill precolored nodes; we thus treat them as having infinite degree 36

37 Effects of Global Register Allocation Reduction in % for MIPS C Compiler Program cycles total loads/stores scalar loads/stores boyer diff yacc nroff ccom upas as Geo Mean

38 Managing Caches Compilers are very good at managing registers Much better than a programmer could be Compilers are not good at managing caches This problem is still left to programmers It is still an open question whether a compiler can do anything general to improve performance Compilers can, and a few do, perform some simple cache optimization 38

39 Cache Optimization Consider the loop for (j = 1; j < 10; j++) for (i = 1; i < ; i++) a[i] *= b[i] This program has terrible cache performance Why? 39

40 Cache Optimization (Cont.) Consider now the program: for (i = 1; i < ; i++) for (j = 1; j < 10; j++) a[i] *= b[i] Computes the same thing But with much better cache behavior Might actually be more than 10x faster A compiler can perform this optimization called loop interchange 40

41 Concluding Remarks Register allocation is a must have optimization in most compilers: Because intermediate code uses too many temporaries Because it makes a big difference in performance Graph coloring is a powerful register allocation scheme (with many variations on the heuristics) Register allocation is more complicated for CISC machines 41

Register Allocation (via graph coloring) Lecture 25. CS 536 Spring

Register Allocation (via graph coloring) Lecture 25. CS 536 Spring Register Allocation (via graph coloring) Lecture 25 CS 536 Spring 2001 1 Lecture Outline Memory Hierarchy Management Register Allocation Register interference graph Graph coloring heuristics Spilling Cache

More information

Register Allocation 1

Register Allocation 1 Register Allocation 1 Lecture Outline Memory Hierarchy Management Register Allocation Register interference graph Graph coloring heuristics Spilling Cache Management The Memory Hierarchy Registers < 1

More information

Register Allocation. Lecture 38

Register Allocation. Lecture 38 Register Allocation Lecture 38 (from notes by G. Necula and R. Bodik) 4/27/08 Prof. Hilfinger CS164 Lecture 38 1 Lecture Outline Memory Hierarchy Management Register Allocation Register interference graph

More information

Register Allocation. Lecture 16

Register Allocation. Lecture 16 Register Allocation Lecture 16 1 Register Allocation This is one of the most sophisticated things that compiler do to optimize performance Also illustrates many of the concepts we ve been discussing in

More information

Lecture 25: Register Allocation

Lecture 25: Register Allocation Lecture 25: Register Allocation [Adapted from notes by R. Bodik and G. Necula] Topics: Memory Hierarchy Management Register Allocation: Register interference graph Graph coloring heuristics Spilling Cache

More information

Register allocation. TDT4205 Lecture 31

Register allocation. TDT4205 Lecture 31 1 Register allocation TDT4205 Lecture 31 2 Variables vs. registers TAC has any number of variables Assembly code has to deal with memory and registers Compiler back end must decide how to juggle the contents

More information

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices Register allocation IR instruction selection register allocation machine code errors Register allocation: have value in a register when used limited resources changes instruction choices can move loads

More information

Register allocation. instruction selection. machine code. register allocation. errors

Register allocation. instruction selection. machine code. register allocation. errors Register allocation IR instruction selection register allocation machine code errors Register allocation: have value in a register when used limited resources changes instruction choices can move loads

More information

Fall Compiler Principles Lecture 12: Register Allocation. Roman Manevich Ben-Gurion University

Fall Compiler Principles Lecture 12: Register Allocation. Roman Manevich Ben-Gurion University Fall 2014-2015 Compiler Principles Lecture 12: Register Allocation Roman Manevich Ben-Gurion University Syllabus Front End Intermediate Representation Optimizations Code Generation Scanning Lowering Local

More information

Variables vs. Registers/Memory. Simple Approach. Register Allocation. Interference Graph. Register Allocation Algorithm CS412/CS413

Variables vs. Registers/Memory. Simple Approach. Register Allocation. Interference Graph. Register Allocation Algorithm CS412/CS413 Variables vs. Registers/Memory CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 33: Register Allocation 18 Apr 07 Difference between IR and assembly code: IR (and abstract assembly) manipulate

More information

CSE P 501 Compilers. Register Allocation Hal Perkins Autumn /22/ Hal Perkins & UW CSE P-1

CSE P 501 Compilers. Register Allocation Hal Perkins Autumn /22/ Hal Perkins & UW CSE P-1 CSE P 501 Compilers Register Allocation Hal Perkins Autumn 2011 11/22/2011 2002-11 Hal Perkins & UW CSE P-1 Agenda Register allocation constraints Local methods Faster compile, slower code, but good enough

More information

Register Allocation & Liveness Analysis

Register Allocation & Liveness Analysis Department of Computer Sciences Register Allocation & Liveness Analysis CS502 Purdue University is an Equal Opportunity/Equal Access institution. Department of Computer Sciences In IR tree code generation,

More information

Topic 12: Register Allocation

Topic 12: Register Allocation Topic 12: Register Allocation COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Structure of backend Register allocation assigns machine registers (finite supply!) to virtual

More information

Register Allocation. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Register Allocation. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Register Allocation Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP at Rice. Copyright 00, Keith D. Cooper & Linda Torczon, all rights reserved.

More information

Liveness Analysis and Register Allocation. Xiao Jia May 3 rd, 2013

Liveness Analysis and Register Allocation. Xiao Jia May 3 rd, 2013 Liveness Analysis and Register Allocation Xiao Jia May 3 rd, 2013 1 Outline Control flow graph Liveness analysis Graph coloring Linear scan 2 Basic Block The code in a basic block has: one entry point,

More information

register allocation saves energy register allocation reduces memory accesses.

register allocation saves energy register allocation reduces memory accesses. Lesson 10 Register Allocation Full Compiler Structure Embedded systems need highly optimized code. This part of the course will focus on Back end code generation. Back end: generation of assembly instructions

More information

Low-Level Issues. Register Allocation. Last lecture! Liveness analysis! Register allocation. ! More register allocation. ! Instruction scheduling

Low-Level Issues. Register Allocation. Last lecture! Liveness analysis! Register allocation. ! More register allocation. ! Instruction scheduling Low-Level Issues Last lecture! Liveness analysis! Register allocation!today! More register allocation!later! Instruction scheduling CS553 Lecture Register Allocation I 1 Register Allocation!Problem! Assign

More information

Lecture 6. Register Allocation. I. Introduction. II. Abstraction and the Problem III. Algorithm

Lecture 6. Register Allocation. I. Introduction. II. Abstraction and the Problem III. Algorithm I. Introduction Lecture 6 Register Allocation II. Abstraction and the Problem III. Algorithm Reading: Chapter 8.8.4 Before next class: Chapter 10.1-10.2 CS243: Register Allocation 1 I. Motivation Problem

More information

Register Allocation. CS 502 Lecture 14 11/25/08

Register Allocation. CS 502 Lecture 14 11/25/08 Register Allocation CS 502 Lecture 14 11/25/08 Where we are... Reasonably low-level intermediate representation: sequence of simple instructions followed by a transfer of control. a representation of static

More information

CSC D70: Compiler Optimization Register Allocation

CSC D70: Compiler Optimization Register Allocation CSC D70: Compiler Optimization Register Allocation Prof. Gennady Pekhimenko University of Toronto Winter 2018 The content of this lecture is adapted from the lectures of Todd Mowry and Phillip Gibbons

More information

Outline. Register Allocation. Issues. Storing values between defs and uses. Issues. Issues P3 / 2006

Outline. Register Allocation. Issues. Storing values between defs and uses. Issues. Issues P3 / 2006 P3 / 2006 Register Allocation What is register allocation Spilling More Variations and Optimizations Kostis Sagonas 2 Spring 2006 Storing values between defs and uses Program computes with values value

More information

Lecture Notes on Register Allocation

Lecture Notes on Register Allocation Lecture Notes on Register Allocation 15-411: Compiler Design Frank Pfenning Lecture 3 September 1, 2009 1 Introduction In this lecture we discuss register allocation, which is one of the last steps in

More information

Register Allocation. Ina Schaefer Selected Aspects of Compilers 100. Register Allocation

Register Allocation. Ina Schaefer Selected Aspects of Compilers 100. Register Allocation Efficient code has to use the available registers on the target machine as much as possible: Accessing registers is much faster then accessing memory (the same holds for cache). Two Aspects: : Determine

More information

More Code Generation and Optimization. Pat Morin COMP 3002

More Code Generation and Optimization. Pat Morin COMP 3002 More Code Generation and Optimization Pat Morin COMP 3002 Outline DAG representation of basic blocks Peephole optimization Register allocation by graph coloring 2 Basic Blocks as DAGs 3 Basic Blocks as

More information

Compiler Architecture

Compiler Architecture Code Generation 1 Compiler Architecture Source language Scanner (lexical analysis) Tokens Parser (syntax analysis) Syntactic structure Semantic Analysis (IC generator) Intermediate Language Code Optimizer

More information

Lecture 21 CIS 341: COMPILERS

Lecture 21 CIS 341: COMPILERS Lecture 21 CIS 341: COMPILERS Announcements HW6: Analysis & Optimizations Alias analysis, constant propagation, dead code elimination, register allocation Available Soon Due: Wednesday, April 25 th Zdancewic

More information

Compiler Design. Register Allocation. Hwansoo Han

Compiler Design. Register Allocation. Hwansoo Han Compiler Design Register Allocation Hwansoo Han Big Picture of Code Generation Register allocation Decides which values will reside in registers Changes the storage mapping Concerns about placement of

More information

Chapter 9. Register Allocation

Chapter 9. Register Allocation Chapter 9. Register Allocation Basics of Compiler Design Torben Ægidius Mogensen Dr. Marco Valtorta, Professor Computer Science and Engineering Dept. University of South Carolina Radu Vitoc, PhD candidate

More information

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z) G22.2130-001 Compiler Construction Lecture 12: Code Generation I Mohamed Zahran (aka Z) mzahran@cs.nyu.edu semantically equivalent + symbol table Requirements Preserve semantic meaning of source program

More information

Lecture 4: RISC Computers

Lecture 4: RISC Computers Lecture 4: RISC Computers Introduction Program execution features RISC characteristics RISC vs. CICS Zebo Peng, IDA, LiTH 1 Introduction Reduced Instruction Set Computer (RISC) represents an important

More information

Global Register Allocation

Global Register Allocation Global Register Allocation Y N Srikant Computer Science and Automation Indian Institute of Science Bangalore 560012 NPTEL Course on Compiler Design Outline n Issues in Global Register Allocation n The

More information

Compilers and Code Optimization EDOARDO FUSELLA

Compilers and Code Optimization EDOARDO FUSELLA Compilers and Code Optimization EDOARDO FUSELLA Contents Data memory layout Instruction selection Register allocation Data memory layout Memory Hierarchy Capacity vs access speed Main memory Classes of

More information

Intermediate Code & Local Optimizations

Intermediate Code & Local Optimizations Lecture Outline Intermediate Code & Local Optimizations Intermediate code Local optimizations Compiler Design I (2011) 2 Code Generation Summary We have so far discussed Runtime organization Simple stack

More information

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example Lecture 36: Local Optimization [Adapted from notes by R. Bodik and G. Necula] Introduction to Code Optimization Code optimization is the usual term, but is grossly misnamed, since code produced by optimizers

More information

Register Allocation. Introduction Local Register Allocators

Register Allocation. Introduction Local Register Allocators Register Allocation Introduction Local Register Allocators Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit

More information

Global Register Allocation - Part 3

Global Register Allocation - Part 3 Global Register Allocation - Part 3 Y N Srikant Computer Science and Automation Indian Institute of Science Bangalore 560012 NPTEL Course on Compiler Design Outline Issues in Global Register Allocation

More information

1 Motivation for Improving Matrix Multiplication

1 Motivation for Improving Matrix Multiplication CS170 Spring 2007 Lecture 7 Feb 6 1 Motivation for Improving Matrix Multiplication Now we will just consider the best way to implement the usual algorithm for matrix multiplication, the one that take 2n

More information

Lecture Overview Register Allocation

Lecture Overview Register Allocation 1 Lecture Overview Register Allocation [Chapter 13] 2 Introduction Registers are the fastest locations in the memory hierarchy. Often, they are the only memory locations that most operations can access

More information

SE-292 High Performance Computing. Memory Hierarchy. R. Govindarajan Memory Hierarchy

SE-292 High Performance Computing. Memory Hierarchy. R. Govindarajan Memory Hierarchy SE-292 High Performance Computing Memory Hierarchy R. Govindarajan govind@serc Memory Hierarchy 2 1 Memory Organization Memory hierarchy CPU registers few in number (typically 16/32/128) subcycle access

More information

CS 2461: Computer Architecture 1

CS 2461: Computer Architecture 1 Next.. : Computer Architecture 1 Performance Optimization CODE OPTIMIZATION Code optimization for performance A quick look at some techniques that can improve the performance of your code Rewrite code

More information

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky Compilation 0368-3133 2016/17a Lecture 10 Register Allocation Noam Rinetzky 1 What is a Compiler? 2 Registers Dedicated memory locations that can be accessed quickly, can have computations performed on

More information

Compiler Optimization and Code Generation

Compiler Optimization and Code Generation Compiler Optimization and Code Generation Professor: Sc.D., Professor Vazgen elikyan 1 Course Overview ntroduction: Overview of Optimizations 1 lecture ntermediate-code Generation 2 lectures achine-ndependent

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Register Allocation. Preston Briggs Reservoir Labs

Register Allocation. Preston Briggs Reservoir Labs Register Allocation Preston Briggs Reservoir Labs An optimizing compiler SL FE IL optimizer IL BE SL A classical optimizing compiler (e.g., LLVM) with three parts and a nice separation of concerns: front

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

Global Register Allocation via Graph Coloring

Global Register Allocation via Graph Coloring Global Register Allocation via Graph Coloring Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission

More information

Abstract Interpretation Continued

Abstract Interpretation Continued Abstract Interpretation Continued Height of Lattice: Length of Max. Chain height=5 size=14 T height=2 size = T -2-1 0 1 2 Chain of Length n A set of elements x 0,x 1,..., x n in D that are linearly ordered,

More information

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Traditional Three-pass Compiler

More information

So, coming back to this picture where three levels of memory are shown namely cache, primary memory or main memory and back up memory.

So, coming back to this picture where three levels of memory are shown namely cache, primary memory or main memory and back up memory. Computer Architecture Prof. Anshul Kumar Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture - 31 Memory Hierarchy: Virtual Memory In the memory hierarchy, after

More information

Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu.

Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu. Optimization ASU Textbook Chapter 9 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine.

More information

CSE 504: Compiler Design. Code Generation

CSE 504: Compiler Design. Code Generation Code Generation Pradipta De pradipta.de@sunykorea.ac.kr Current Topic Introducing basic concepts in code generation phase Code Generation Detailed Steps The problem of generating an optimal target program

More information

Naming in OOLs and Storage Layout Comp 412

Naming in OOLs and Storage Layout Comp 412 COMP 412 FALL 2018 Naming in OOLs and Storage Layout Comp 412 source IR IR target Front End Optimizer Back End Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in

More information

PERFORMANCE OPTIMISATION

PERFORMANCE OPTIMISATION PERFORMANCE OPTIMISATION Adrian Jackson a.jackson@epcc.ed.ac.uk @adrianjhpc Hardware design Image from Colfax training material Pipeline Simple five stage pipeline: 1. Instruction fetch get instruction

More information

CODE GENERATION Monday, May 31, 2010

CODE GENERATION Monday, May 31, 2010 CODE GENERATION memory management returned value actual parameters commonly placed in registers (when possible) optional control link optional access link saved machine status local data temporaries A.R.

More information

Register allocation. Overview

Register allocation. Overview Register allocation Register allocation Overview Variables may be stored in the main memory or in registers. { Main memory is much slower than registers. { The number of registers is strictly limited.

More information

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V. Register allocation CS3300 - Compiler Design Liveness analysis and Register allocation V. Krishna Nandivada IIT Madras Copyright c 2014 by Antony L. Hosking. Permission to make digital or hard copies of

More information

University of California at Santa Barbara. ECE 154A Introduction to Computer Architecture. Quiz #1. October 30 th, Name (Last, First)

University of California at Santa Barbara. ECE 154A Introduction to Computer Architecture. Quiz #1. October 30 th, Name (Last, First) University of California at Santa Barbara ECE 154A Introduction to Computer Architecture Quiz #1 October 30 th, 2012 Name (Last, First) All grades will be posted on the website as a single spreadsheet

More information

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573 What is a compiler? Xiaokang Qiu Purdue University ECE 573 August 21, 2017 What is a compiler? What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g.,

More information

Exercises 6 - Virtual vs. Physical Memory, Cache

Exercises 6 - Virtual vs. Physical Memory, Cache Questions 1. Consider a (very old!) computer with a hard disk of only 1 GB, main memory of 16 MB, and a cache with 512 blocks of 8 words each. Suppose the hard disk and main memory are partitioned into

More information

Register Allocation. Register Allocation. Local Register Allocation. Live range. Register Allocation for Loops

Register Allocation. Register Allocation. Local Register Allocation. Live range. Register Allocation for Loops DF00100 Advanced Compiler Construction Register Allocation Register Allocation: Determines values (variables, temporaries, constants) to be kept when in registers Register Assignment: Determine in which

More information

ECE 486/586. Computer Architecture. Lecture # 7

ECE 486/586. Computer Architecture. Lecture # 7 ECE 486/586 Computer Architecture Lecture # 7 Spring 2015 Portland State University Lecture Topics Instruction Set Principles Instruction Encoding Role of Compilers The MIPS Architecture Reference: Appendix

More information

Lecture 15 Register Allocation & Spilling

Lecture 15 Register Allocation & Spilling I. Motivation Lecture 15 Register Allocation & Spilling I. Introduction II. Abstraction and the Problem III. Algorithm IV. Spilling Problem Allocation of variables (pseudo-registers) to hardware registers

More information

Low-level optimization

Low-level optimization Low-level optimization Advanced Course on Compilers Spring 2015 (III-V): Lecture 6 Vesa Hirvisalo ESG/CSE/Aalto Today Introduction to code generation finding the best translation Instruction selection

More information

Optimising for the p690 memory system

Optimising for the p690 memory system Optimising for the p690 memory Introduction As with all performance optimisation it is important to understand what is limiting the performance of a code. The Power4 is a very powerful micro-processor

More information

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS Lectures 5 Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS 1 OOPS - What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; } 2

More information

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction Procedure Abstraction Run Time Environment Records Procedure Linkage Name Translation and Variable Access Copyright 2010, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at

More information

Intermediate Code & Local Optimizations. Lecture 20

Intermediate Code & Local Optimizations. Lecture 20 Intermediate Code & Local Optimizations Lecture 20 Lecture Outline Intermediate code Local optimizations Next time: global optimizations 2 Code Generation Summary We have discussed Runtime organization

More information

Lecture 4: RISC Computers

Lecture 4: RISC Computers Lecture 4: RISC Computers Introduction Program execution features RISC characteristics RISC vs. CICS Zebo Peng, IDA, LiTH 1 Introduction Reduced Instruction Set Computer (RISC) is an important innovation

More information

ECE/CS 552: Introduction to Computer Architecture ASSIGNMENT #1 Due Date: At the beginning of lecture, September 22 nd, 2010

ECE/CS 552: Introduction to Computer Architecture ASSIGNMENT #1 Due Date: At the beginning of lecture, September 22 nd, 2010 ECE/CS 552: Introduction to Computer Architecture ASSIGNMENT #1 Due Date: At the beginning of lecture, September 22 nd, 2010 This homework is to be done individually. Total 9 Questions, 100 points 1. (8

More information

Code Generation. CS 540 George Mason University

Code Generation. CS 540 George Mason University Code Generation CS 540 George Mason University Compiler Architecture Intermediate Language Intermediate Language Source language Scanner (lexical analysis) tokens Parser (syntax analysis) Syntactic structure

More information

Computer Architecture, RISC vs. CISC, and MIPS Processor

Computer Architecture, RISC vs. CISC, and MIPS Processor CSE 2421: Systems I Low-Level Programming and Computer Organization Computer Architecture, RISC vs. CISC, and MIPS Processor Gojko Babić 1-1-217 Computer Architecture A modern meaning of the term computer

More information

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations Register Allocation Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class

More information

William Stallings Computer Organization and Architecture. Chapter 12 Reduced Instruction Set Computers

William Stallings Computer Organization and Architecture. Chapter 12 Reduced Instruction Set Computers William Stallings Computer Organization and Architecture Chapter 12 Reduced Instruction Set Computers Major Advances in Computers(1) The family concept IBM System/360 1964 DEC PDP-8 Separates architecture

More information

Agenda. Cache-Memory Consistency? (1/2) 7/14/2011. New-School Machine Structures (It s a bit more complicated!)

Agenda. Cache-Memory Consistency? (1/2) 7/14/2011. New-School Machine Structures (It s a bit more complicated!) 7/4/ CS 6C: Great Ideas in Computer Architecture (Machine Structures) Caches II Instructor: Michael Greenbaum New-School Machine Structures (It s a bit more complicated!) Parallel Requests Assigned to

More information

Lecture 5. Announcements: Today: Finish up functions in MIPS

Lecture 5. Announcements: Today: Finish up functions in MIPS Lecture 5 Announcements: Today: Finish up functions in MIPS 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In

More information

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations Lecture Outline Intermediate code Intermediate Code & Local Optimizations Lecture 14 Local optimizations Next time: global optimizations Prof. Aiken CS 143 Lecture 14 1 Prof. Aiken CS 143 Lecture 14 2

More information

Code Generation & Parameter Passing

Code Generation & Parameter Passing Code Generation & Parameter Passing Lecture Outline 1. Allocating temporaries in the activation record Let s optimize our code generator a bit 2. A deeper look into calling sequences Caller/Callee responsibilities

More information

RISC Principles. Introduction

RISC Principles. Introduction 3 RISC Principles In the last chapter, we presented many details on the processor design space as well as the CISC and RISC architectures. It is time we consolidated our discussion to give details of RISC

More information

Multi-dimensional Arrays

Multi-dimensional Arrays Multi-dimensional Arrays IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula) A 2D array is a 1D array of 1D arrays Java uses arrays of pointers to arrays for >1D

More information

Run-time Environment

Run-time Environment Run-time Environment Prof. James L. Frankel Harvard University Version of 3:08 PM 20-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Storage Organization Automatic objects are

More information

Calvin Lin The University of Texas at Austin

Calvin Lin The University of Texas at Austin Loop Invariant Code Motion Last Time SSA Today Loop invariant code motion Reuse optimization Next Time More reuse optimization Common subexpression elimination Partial redundancy elimination February 23,

More information

Loop Transformations! Part II!

Loop Transformations! Part II! Lecture 9! Loop Transformations! Part II! John Cavazos! Dept of Computer & Information Sciences! University of Delaware! www.cis.udel.edu/~cavazos/cisc879! Loop Unswitching Hoist invariant control-flow

More information

Agenda. EE 260: Introduction to Digital Design Memory. Naive Register File. Agenda. Memory Arrays: SRAM. Memory Arrays: Register File

Agenda. EE 260: Introduction to Digital Design Memory. Naive Register File. Agenda. Memory Arrays: SRAM. Memory Arrays: Register File EE 260: Introduction to Digital Design Technology Yao Zheng Department of Electrical Engineering University of Hawaiʻi at Mānoa 2 Technology Naive Register File Write Read clk Decoder Read Write 3 4 Arrays:

More information

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,... CSE 401 Final Exam March 14, 2017 Happy π Day! (3/14) Name This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,.... Please wait to turn the page until everyone has their

More information

Global Register Allocation - 2

Global Register Allocation - 2 Global Register Allocation - 2 Y N Srikant Computer Science and Automation Indian Institute of Science Bangalore 560012 NPTEL Course on Principles of Compiler Design Outline n Issues in Global Register

More information

COMP3221: Microprocessors and. and Embedded Systems. Instruction Set Architecture (ISA) What makes an ISA? #1: Memory Models. What makes an ISA?

COMP3221: Microprocessors and. and Embedded Systems. Instruction Set Architecture (ISA) What makes an ISA? #1: Memory Models. What makes an ISA? COMP3221: Microprocessors and Embedded Systems Lecture 2: Instruction Set Architecture (ISA) http://www.cse.unsw.edu.au/~cs3221 Lecturer: Hui Wu Session 2, 2005 Instruction Set Architecture (ISA) ISA is

More information

ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design ECE232: Hardware Organization and Design Lecture 2: Hardware/Software Interface Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Overview Basic computer components How does a microprocessor

More information

CSE351 Winter 2016, Final Examination March 16, 2016

CSE351 Winter 2016, Final Examination March 16, 2016 CSE351 Winter 2016, Final Examination March 16, 2016 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. Please stop promptly at 4:20. There are 125 (not 100) points,

More information

16.10 Exercises. 372 Chapter 16 Code Improvement. be translated as

16.10 Exercises. 372 Chapter 16 Code Improvement. be translated as 372 Chapter 16 Code Improvement 16.10 Exercises 16.1 In Section 16.2 we suggested replacing the instruction r1 := r2 / 2 with the instruction r1 := r2 >> 1, and noted that the replacement may not be correct

More information

Run-time Environments -Part 1

Run-time Environments -Part 1 Run-time Environments -Part 1 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Outline of the Lecture Part 1 What is run-time support?

More information

April 15, 2015 More Register Allocation 1. Problem Register values may change across procedure calls The allocator must be sensitive to this

April 15, 2015 More Register Allocation 1. Problem Register values may change across procedure calls The allocator must be sensitive to this More Register Allocation Last time Register allocation Global allocation via graph coloring Today More register allocation Procedure calls Interprocedural April 15, 2015 More Register Allocation 1 Register

More information

MODEL ANSWERS COMP36512, May 2016

MODEL ANSWERS COMP36512, May 2016 MODEL ANSWERS COMP36512, May 2016 QUESTION 1: a) Clearly: 1-g, 2-d, 3-h, 4-e, 5-i, 6-a, 7-b, 8-c, 9-f. 0.5 marks for each correct answer rounded up as no halves are used. b) i) It has been mentioned in

More information

OPERATING SYSTEMS. After A.S.Tanenbaum, Modern Operating Systems 3rd edition Uses content with permission from Assoc. Prof. Florin Fortis, PhD

OPERATING SYSTEMS. After A.S.Tanenbaum, Modern Operating Systems 3rd edition Uses content with permission from Assoc. Prof. Florin Fortis, PhD OPERATING SYSTEMS #8 After A.S.Tanenbaum, Modern Operating Systems 3rd edition Uses content with permission from Assoc. Prof. Florin Fortis, PhD MEMORY MANAGEMENT MEMORY MANAGEMENT The memory is one of

More information

Heap Management. Heap Allocation

Heap Management. Heap Allocation Heap Management Heap Allocation A very flexible storage allocation mechanism is heap allocation. Any number of data objects can be allocated and freed in a memory pool, called a heap. Heap allocation is

More information

CSCI Compiler Design

CSCI Compiler Design CSCI 565 - Compiler Design Spring 2010 Final Exam - Solution May 07, 2010 at 1.30 PM in Room RTH 115 Duration: 2h 30 min. Please label all pages you turn in with your name and student number. Name: Number:

More information

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done What is a compiler? What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g., C++) to low-level assembly language that can be executed by hardware int a,

More information

Functions in MIPS. Functions in MIPS 1

Functions in MIPS. Functions in MIPS 1 Functions in MIPS We ll talk about the 3 steps in handling function calls: 1. The program s flow of control must be changed. 2. Arguments and return values are passed back and forth. 3. Local variables

More information

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information