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

Size: px
Start display at page:

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

Transcription

1 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 which variables are implemented by registers at which positions. Register Assignment: Determine which register implements which variable at which positions. With register allocation, we mean both aspects. Ina Schaefer Selected Aspects of Compilers 100 (2) Goals of 1. Generate code that requires as little registers as possible 2. Avoid unnecessary memory accesses, i.e., not only temporaries, but also program variables are implemented by registers. 3. Allocate registers such that they can be used as much as possible, i.e., registers should not be used for variables that are only rarely accessed. 4. Obey programmer s requirements. Ina Schaefer Selected Aspects of Compilers 101

2 (3) Outline Algorithm interleaving code generation and register allocation for nested expressions (cf. Goal 1) Algorithm for procedure-local register allocation (cf. Goals 2 and 3) Combination and other aspects Ina Schaefer Selected Aspects of Compilers 102 The algorithm by Sethi and Ullmann is an example of an integrated approach for register allocation and code generation. (cf. Wilhelm, Maurer, Sect , p. 584 ff) Input: An assignment with a nested expression on the right hand side Assign ( Var, Exp ) Exp = BinExp Var BinExp ( Exp, Op, Exp ) Var ( Ident ) Ina Schaefer Selected Aspects of Compilers 103

3 (2) Output: Machine or intermediate language code with assigned registers. We consider two-address code, i.e., code with one memory access at maximum. The machine has r registers represented by R 0,..., R r 1. Ri := M[V] M[V] := Ri Ri := Ri op M[V] Ri := Ri op Rj Ina Schaefer Selected Aspects of Compilers 104 Example: Code Generation w/ Consider f := (a + b) (c (d + e)) Assume that there are two registers R0 and R1 available for the translation. Result of direct translation: R0 := M[a] R0 := R0 + M[b] R1 := M[d] R1 := R1 + M[e] M[t1] := R1 R1 := M[c] R1 := R1 M[t1] R0 := R0 R1 M[f] := R0 Ina Schaefer Selected Aspects of Compilers 105

4 Example: Code Generation w/ (2) Result of Sethi-Ullmann-Algorithm: R0 := M[c] R1 := M[d] R1 := R1 + M[e] R0 := R0 R1 R1 := M[a] R1 := R1 + M[b] R1 := R1 R0 M[f] := R1 More efficient, because it uses one instruction less and does not need to store intermediate results. Ina Schaefer Selected Aspects of Compilers 106 Sethi-Ullmann Algorithm Goal: Minimize number of registers and number of temporaries. Idea: Generate code for subexpression requiring more registers first. Procedure: Define function regbed that computes the number of registers needed for an expression Generate code for an expression E = BinExp(L,OP,R); Ina Schaefer Selected Aspects of Compilers 107

5 Sethi-Ullmann Algorithm (2) We use the following notations: v_reg(e): the set of available registers for the translation of E v_tmp(e): the set of addresses where values can be stored temporarily when translating E cell(e): register/memory cell where the result of E is stored vr = v_reg(e) denotes the number of available registers Ina Schaefer Selected Aspects of Compilers 108 Sethi-Ullmann Algorithm (3) We distinguish the following cases: 1. regbed(l) < vr 2. regbed(l) vr and regbed(r) < vr 3. regbed(l) vr and redbed(r) vr Ina Schaefer Selected Aspects of Compilers 109

6 Sethi-Ullmann Algorithm (4) Case 1: regbed(l) < vr Generate code for R using v_reg(e) and v_tmp(e) with result in cell(r) Generate code for L using v_reg(e) \{ cell(r) } and v_tmp(e) with result in cell(l) Generate code for the operation cell(l) := cell(l) OP cell(r) Set cell(e) = cell(l) Ina Schaefer Selected Aspects of Compilers 110 Sethi-Ullmann Algorithm (5) Case 2: regbed(l) vr and regbed(r) < vr Generate code for L using v_reg(e) and v_tmp(e) with result in cell(l) Generate code for R using v_reg(e) \{ cell(l) } and v_tmp(e) with result in cell(r) Generate code for the operation cell(l) := cell(l) OP cell(r) Set cell(e) = cell(l) Ina Schaefer Selected Aspects of Compilers 111

7 Sethi-Ullmann Algorithm (6) Case 3: regbed(l) vr and redbed(r) vr Generate code for R using v_reg(e) and v_tmp(e) with result in cell(r) Generate code M[first(v_tmp(E))] := cell(r) Generate code for L using v_reg(e) and rest(v_tmp(e)) with result in cell(l) Generate code for the operation cell(l) := cell(l) OP M[first(v_tmp(E))] Set cell(e) = cell(l) Ina Schaefer Selected Aspects of Compilers 112 Sethi-Ullmann Algorithm (7) Function regbed in MAX Notation (can be realized by S-Attribution): ATT regbed( Exp@ E ) Nat: IF Assign@<_,Var@ E> : 0 BinExp@< Var@ E,_,_> : 1 BinExp@<_,_,Var@ E > : 0 BinExp@< L,_, R > E : IF regbed(l)=regbed(r) THEN regbed(l) + 1 ELSE max( regbed(l), regbed(r) ) ELSE nil // Fall kommt nicht vor Ina Schaefer Selected Aspects of Compilers 113

8 Example: Sethi-Ullman Algorithm Consider f:= (( a + b ) - (c + d)) * (a - (d+e)) Attributes: v_reg v_tmp regbed zelle 12T Ina Schaefer Selected Aspects of Compilers 114 Example: Sethi-Ullman Algorithm (2) Assign Var f 12T BinExp * (3.) 3 1 T BinExp 12T BinExp (1.) (1.) 2 BinExp 2 12 BinExp 1 Var 12T BinExp a (2.) (1.) (1.) Var a Var Var Var Var Var 1 b 0 c 1 d 0 d 1 e 0 Ina Schaefer Selected Aspects of Compilers 115

9 Example: Sethi-Ullman Algorithm (3) For formalizing the algorithm, we realize the set of available registers and addresses for storing temporaries with lists, where the list RL of registers is non-empty the list AL of addresses is long enough the result cell is always a register which is the first in RL, i.e., first(rl) the function exchange switches the first two elements of a list, fst returns the first element of the list, rest returns the tail of the list Ina Schaefer Selected Aspects of Compilers 116 Example: Sethi-Ullman Algorithm (4) Remarks: The algorithm generates 2AC which is optimal with respect to the number of instructions and the number of temporaries if the expression has no common subexpressions. The algorithm shows the dependency between code generation and register allocation and vice versa. In a procedural implementation, register and address lists can be realized by a global stack. Ina Schaefer Selected Aspects of Compilers 117

10 Example: Sethi-Ullman Algorithm (5) In the following, the function expcode for code generation is given in MAX Notation (functional). Note: The application of the functions exchange, fst and expcode satisfy their preconditions length(rl) > 1 or length(rl) > 0, resp. Ina Schaefer Selected Aspects of Compilers 118 Example: Sethi-Ullman Algorithm (6) FCT expcode( Exp@ E, RegList RL, AdrList AL ) CodeList: // pre: length(rl)>0 IF Var@<ID> E: [ fst(rl) := M[adr(ID)] ] BinExp@< L,OP,Var@<ID> > E: expcode(l,rl,al) ++ [ fst(rl) := fst(rl) OP M[adr(ID)] ] BinExp@< L,OP,R > E: LET vr == length( RL ) : IF regbed(l) < vr : expcode(r,exchange(rl),al) ++ expcode(l,rst(exchange(rl)),al) ++ [ fst(rl):= fst(rl) OP fst(rst(rl))] regbed(l)>=vr AND regbed(r)<vr : expcode(l,rl,al) ++ expcode(r,rst(rl),al) ++ [ fst(rl):= fst(rl) OP fst(rst(rl))] regbed(l)>=vr AND regbed(r)>=vr : expcode(r,rl,al) ++ [ M[ fst(al) ] := fst(rl) ] ++ expcode(l,rl,rst(al)) ++ [ fst(rl):= fst(rl) OP M[fst(AL)] ] ELSE nil ELSE [] Ina Schaefer Selected Aspects of Compilers 119

11 by Graph Coloring by Graph Coloring Register allocation by graph coloring is a procedure (with many variants) for allocation of registers beyond expressions and basic blocks. for 3AC Input: 3AC in SSA with temporary variables Output: Structurally the same SSA with registers instead of temporary variables additional instructions for storing intermediate results on the stack, if applicable Ina Schaefer Selected Aspects of Compilers 120 by Graph Coloring by Graph Coloring (2) Remarks: The SSA representation is not necessary, but simplifies the formulation of the algorithm (e.g.,wilhelm/maurer in Sect do not use SSA) It is no restriction that only temporary variables are implemented by registers. We assume that program variables are assigned to temporary variables as well, if appropriate. Ina Schaefer Selected Aspects of Compilers 121

12 Life Range and Interference Graph by Graph Coloring Definition (Life Range) The life range of a temporary variable is the set of program positions at which it is live. Definition (Interference) Two temporary variables interfere if their life ranges have a non-empty intersection Definition (Interference Graph) Let P be a program part in 3AC/SSA. The interference graph of P is an undirected graph G =(N, E), where N is the set of temporary variables E is an edge (n 1, n 2 ) iff n 1 and n 2 interfere. Ina Schaefer Selected Aspects of Compilers 122 by Graph Coloring by Graph Coloring Goal: Reduce number of temporary variables with the available registers. Idea: Translate the problem to graph coloring (NP-complete). Color the interference graph, such that neighboring nodes have differing colors. no more colors are used than available registers. Ina Schaefer Selected Aspects of Compilers 123

13 by Graph Coloring by Graph Coloring (2) General Procedure: For coloring the graph, we have two cases: If a coloring is found, terminate. If nodes could not be colored, choose a non-colored node k modify the 3AC program such that the value of k is stored temporarily and is first loaded when it is applied try to find a new coloring Termination: The procedure terminates, because by temporarily storing the life ranges and the interferences are reduced. In practice, two or three iterations are sufficient. Ina Schaefer Selected Aspects of Compilers 124 by Graph Coloring by Graph Coloring (3) Coloring Procedure: Let rd be the number of available registers, i.e., for coloring, maximally rn colors may be used. The coloring procedure consists of the steps: (a) Simplify by Marking (b) Coloring Ina Schaefer Selected Aspects of Compilers 125

14 Simplify by Marking by Graph Coloring Remove iteratively nodes with less than rn neighbors from the graph and put them on the stack. Case 1: The current simplification steps leads to an empty graph. Continue with coloring. Case 2: The graph contains only nodes with rn and more than rn neighbors. Choose a suitable node as candidate for storing it temporarily, mark it, put it on the stack and continue simplification. Ina Schaefer Selected Aspects of Compilers 126 Coloring by Graph Coloring The nodes are pushed from the stack in their order and, if possible, colored and put back into the graph. Let k be the node taken from the stack. Case1: k is not marked. Thus, it has less than rn neighbors. Then, k can be colored with a new color. Case2: k is marked. a) the rn or more neighbors have less than rn-1 different colors. Then, color k appropriately. b) there are rn or more colors in the neighborhood. Leave k uncolored. Ina Schaefer Selected Aspects of Compilers 127

15 Example - Graph Coloring by Graph Coloring For simplicity, we only consider one basic block. In the beginning, t0 and t2 are live t1 := a + t0 t3 := t2 1 t4 := t1 * t3 t5 := b + t0 t6 := c + t0 t7 := d + t4 t8 := t5 + 8 t9 := t8 t2 := t6 + 4 t0 := t7 In the end, t0, t2, t9 are live. Ina Schaefer Selected Aspects of Compilers 128 Example - Graph Coloring (2) Interference graph: by Graph Coloring Assumption: 4 available registers Simplification: Remove (in order) t1, t3, t2, t9, t0, t5, t4, t7, t8, t6 Ina Schaefer Selected Aspects of Compilers 129

16 Example - Graph Coloring (3) Possible Coloring: by Graph Coloring t4 t5 t0 t3 t2 t7 t8 t6 t1 t9 Ina Schaefer Selected Aspects of Compilers 130 Example - Graph Coloring (4) by Graph Coloring Remarks: There are several extensions of the procedure: Elimination of move instructions Specific heuristics for simplification (What is a suitable node?) Consider pre-colored nodes Recommended Reading: Appel, Sec Ina Schaefer Selected Aspects of Compilers 131

17 Further Aspects of Further Aspects of The introduced algorithms consider subproblems. In practice, there are further aspects, that have to be dealt with for register allocation: Interaction with other compiler phases (in particular optimization and code generation) Relation between temporaries and registers Source/Intermediate/Target Language Number of applications (Is a variable inside an inner loop?) Ina Schaefer Selected Aspects of Compilers 132 Further Aspects of Further Aspects of (2) Possible global procedure Allocate registers for standard tasks (registers for stack and argument pointers, base registers) Decide which variables and parameters should be stored in registers Evaluate application frequency of temporaries (Occurrences in inner loops, distribution of accesses over life range) Use evaluation together with heuristics of register allocation algorithm If applicable, optimize again Ina Schaefer Selected Aspects of Compilers 133

18 Code Generation Code Generation Code generation can be split into four independent machine-dependent tasks: Memory allocation Instruction selection and addressing Instruction scheduling Code optimization Ina Schaefer Selected Aspects of Compilers 134 Memory Allocation Code Generation Modern machines have the following memory hierarchy: Registers Primary Cache (Instruction Cache, Data Cache) Secondary Cache Main Memory (Page/Segment Addressing) Different from registers, the cache is controlled by the hardware. Efficient usage of the cache means in particular to align data objects and instructions to borders of cache blocks (cf. Appel, Chap. 21). The same holds for main memory. Ina Schaefer Selected Aspects of Compilers 135

19 Instruction Selection Code Generation Instruction selection aims at the best possible translation of expressions and basic blocks using the instruction set of the machine, for instance, using complex addressing modes considering the sizes of constants or the locality of jumps Instruction selection is often formulated as a tree pattern matching problem with costs. (cf. Wilhelm/Maurer, Chap.11) Ina Schaefer Selected Aspects of Compilers 136 Instruction Scheduling Code Generation Modern machines allow processor-local parallel processing (pipeline, super-scalar, VLIW). In order to use this parallel processing, code has to comply toadditional requirements that have to be considered for code generation. (see Appel, Chap. 20; Wilhelm/Maurer, Sect. 12.6) Ina Schaefer Selected Aspects of Compilers 137

20 Code Optimization Code Generation Optimizations of the assembler or machine code may allow an additional increase in program efficiency. (see Wilhelm/Maurer, Sect. 6.9) Ina Schaefer Selected Aspects of Compilers 138

Global Register Allocation

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

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 (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

Programming Language Processor Theory

Programming Language Processor Theory Programming Language Processor Theory Munehiro Takimoto Course Descriptions Method of Evaluation: made through your technical reports Purposes: understanding various theories and implementations of modern

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

Lecture08: Scope and Lexical Address

Lecture08: Scope and Lexical Address Lecture08: Scope and Lexical Address Free and Bound Variables (EOPL 1.3.1) Given an expression E, does a particular variable reference x appear free or bound in that expression? Definition: A variable

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

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 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

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

Code generation for modern processors

Code generation for modern processors Code generation for modern processors Definitions (1 of 2) What are the dominant performance issues for a superscalar RISC processor? Refs: AS&U, Chapter 9 + Notes. Optional: Muchnick, 16.3 & 17.1 Instruction

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

Code generation for modern processors

Code generation for modern processors Code generation for modern processors What are the dominant performance issues for a superscalar RISC processor? Refs: AS&U, Chapter 9 + Notes. Optional: Muchnick, 16.3 & 17.1 Strategy il il il il asm

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

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

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210 Redundant Computation Elimination Optimizations CS2210 Lecture 20 Redundancy Elimination Several categories: Value Numbering local & global Common subexpression elimination (CSE) local & global Loop-invariant

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

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

High-Level Synthesis

High-Level Synthesis High-Level Synthesis 1 High-Level Synthesis 1. Basic definition 2. A typical HLS process 3. Scheduling techniques 4. Allocation and binding techniques 5. Advanced issues High-Level Synthesis 2 Introduction

More information

Garbage Collection. Lecture Compilers SS Dr.-Ing. Ina Schaefer. Software Technology Group TU Kaiserslautern. Ina Schaefer Garbage Collection 1

Garbage Collection. Lecture Compilers SS Dr.-Ing. Ina Schaefer. Software Technology Group TU Kaiserslautern. Ina Schaefer Garbage Collection 1 Garbage Collection Lecture Compilers SS 2009 Dr.-Ing. Ina Schaefer Software Technology Group TU Kaiserslautern Ina Schaefer Garbage Collection 1 Content of Lecture 1. Introduction: Overview and Motivation

More information

Static analysis and all that

Static analysis and all that Static analysis and all that Martin Steffen IfI UiO Spring 2014 uio Static analysis and all that Martin Steffen IfI UiO Spring 2014 uio Plan approx. 15 lectures, details see web-page flexible time-schedule,

More information

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013 A Bad Name Optimization is the process by which we turn a program into a better one, for some definition of better. CS 2210: Optimization This is impossible in the general case. For instance, a fully optimizing

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

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

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

4. Selected Topics in Compiler Construction

4. Selected Topics in Compiler Construction Content of Lecture Compilers and Language Processing Tools Summer Term 011 Prof. Dr. Arnd Poetzsch-Heffter Software Technology Group TU Kaiserslautern c Prof. Dr. Arnd Poetzsch-Heffter 1 1. Introduction.

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

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano Building a Runnable Program and Code Improvement Dario Marasco, Greg Klepic, Tess DiStefano Building a Runnable Program Review Front end code Source code analysis Syntax tree Back end code Target code

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

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

Functional programming languages

Functional programming languages Functional programming languages Part V: functional intermediate representations Xavier Leroy INRIA Paris-Rocquencourt MPRI 2-4, 2015 2017 X. Leroy (INRIA) Functional programming languages MPRI 2-4, 2015

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

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

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

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

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

Register Allocation via Hierarchical Graph Coloring

Register Allocation via Hierarchical Graph Coloring Register Allocation via Hierarchical Graph Coloring by Qunyan Wu A THESIS Submitted in partial fulfillment of the requirements for the degree of MASTER OF SCIENCE IN COMPUTER SCIENCE MICHIGAN TECHNOLOGICAL

More information

Compiler Construction

Compiler Construction Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Recap: Static Data Structures Outline of Lecture 18 Recap:

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

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

Compiler Construction

Compiler Construction Compiler Construction Lecture 18: Code Generation V (Implementation of Dynamic Data Structures) Thomas Noll Lehrstuhl für Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de http://moves.rwth-aachen.de/teaching/ss-14/cc14/

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

Register Allocation 3/16/11. What a Smart Allocator Needs to Do. Global Register Allocation. Global Register Allocation. Outline.

Register Allocation 3/16/11. What a Smart Allocator Needs to Do. Global Register Allocation. Global Register Allocation. Outline. What a Smart Allocator Needs to Do Register Allocation Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations Determine ranges for each variable can benefit from using

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

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

Advanced Database Systems

Advanced Database Systems Lecture IV Query Processing Kyumars Sheykh Esmaili Basic Steps in Query Processing 2 Query Optimization Many equivalent execution plans Choosing the best one Based on Heuristics, Cost Will be discussed

More information

Global Register Allocation - Part 2

Global Register Allocation - Part 2 Global Register Allocation - Part 2 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

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

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages CSc 520 Principles of Programming Languages 32: Procedures Inlining Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg [1]

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

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

Destination-Driven Code Generation R. Kent Dybvig, Robert Hieb, Tom Butler Computer Science Department Indiana University Bloomington, IN Februa

Destination-Driven Code Generation R. Kent Dybvig, Robert Hieb, Tom Butler Computer Science Department Indiana University Bloomington, IN Februa Destination-Driven Code Generation R. Kent Dybvig, Robert Hieb, Tom Butler dyb@cs.indiana.edu Indiana University Computer Science Department Technical Report #302 February 1990 Destination-Driven Code

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

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

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

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

SSA, CPS, ANF: THREE APPROACHES

SSA, CPS, ANF: THREE APPROACHES SSA, CPS, ANF: THREE APPROACHES TO COMPILING FUNCTIONAL LANGUAGES PATRYK ZADARNOWSKI PATRYKZ@CSE.UNSW.EDU.AU PATRYKZ@CSE.UNSW.EDU.AU 1 INTRODUCTION Features: Intermediate Representation: A programming

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

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology MIT 6.035 Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Program Analysis Compile-time reasoning about run-time behavior

More information

Compiler Optimization

Compiler Optimization Compiler Optimization The compiler translates programs written in a high-level language to assembly language code Assembly language code is translated to object code by an assembler Object code modules

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1 An Overview to Compiler Design 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1 Outline An Overview of Compiler Structure Front End Middle End Back End 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 2 Reading

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 Amitabha Sanyal Department of Computer Science & Engineering IIT Bombay

Register Allocation Amitabha Sanyal Department of Computer Science & Engineering IIT Bombay Register Allocation Amitabha Sanyal Department of Computer Science & Engineering IIT Bombay Global Register Allocation Register Allocation : AS 1 Possible Approaches Approach 1: Divide the register set

More information

CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II

CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II Robin Cockett Department of Computer Science University of Calgary Alberta, Canada robin@cpsc.ucalgary.ca Sept. 2007 Introduction to the course

More information

CS 406/534 Compiler Construction Instruction Selection and Global Register Allocation

CS 406/534 Compiler Construction Instruction Selection and Global Register Allocation CS 406/534 Compiler Construction Instruction Selection and Global Register Allocation Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith

More information

Liveness Analysis and Register Allocation

Liveness Analysis and Register Allocation Liveness Analysis and Register Allocation Leonidas Fegaras CSE 5317/4305 L10: Liveness Analysis and Register Allocation 1 Liveness Analysis So far we assumed that we have a very large number of temporary

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 19 Query Optimization Introduction Query optimization Conducted by a query optimizer in a DBMS Goal: select best available strategy for executing query Based on information available Most RDBMSs

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

Code generation scheme for RCMA

Code generation scheme for RCMA Code generation scheme for RCMA Axel Simon July 5th, 2010 1 Revised Specification of the R-CMa We detail what constitutes the Register C-Machine (R-CMa ) and its operations in turn We then detail how the

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

William Stallings Computer Organization and Architecture 8 th Edition. Chapter 11 Instruction Sets: Addressing Modes and Formats

William Stallings Computer Organization and Architecture 8 th Edition. Chapter 11 Instruction Sets: Addressing Modes and Formats William Stallings Computer Organization and Architecture 8 th Edition Chapter 11 Instruction Sets: Addressing Modes and Formats Addressing Modes Immediate Direct Indirect Register Register Indirect Displacement

More information

CMa simple C Abstract Machine

CMa simple C Abstract Machine CMa simple C Abstract Machine CMa architecture An abstract machine has set of instructions which can be executed in an abstract hardware. The abstract hardware may be seen as a collection of certain data

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

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

CS4410 : Spring 2013

CS4410 : Spring 2013 CS4410 : Spring 2013 Next PS: Map Fish code to MIPS code Issues: eliminating compound expressions eliminating variables encoding conditionals, short- circuits, loops type exp = Var of var Int of int Binop

More information

Linked Lists, Stacks, and Queues

Linked Lists, Stacks, and Queues Department of Computer Science and Engineering Chinese University of Hong Kong In a nutshell, a data structure describes how data are stored in memory, in order to facilitate certain operations. In all

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

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

Lecture 4: Instruction Set Design/Pipelining

Lecture 4: Instruction Set Design/Pipelining Lecture 4: Instruction Set Design/Pipelining Instruction set design (Sections 2.9-2.12) control instructions instruction encoding Basic pipelining implementation (Section A.1) 1 Control Transfer Instructions

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

Introduction. Inline Expansion. CSc 553. Principles of Compilation. 29 : Optimization IV. Department of Computer Science University of Arizona

Introduction. Inline Expansion. CSc 553. Principles of Compilation. 29 : Optimization IV. Department of Computer Science University of Arizona CSc 553 Principles of Compilation 29 : Optimization IV Introduction Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg Inline Expansion I Inline

More information

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker! 7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker! Roadmap > Introduction! > Optimizations in the Back-end! > The Optimizer! > SSA Optimizations! > Advanced Optimizations! 2 Literature!

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

Query Processing & Optimization

Query Processing & Optimization Query Processing & Optimization 1 Roadmap of This Lecture Overview of query processing Measures of Query Cost Selection Operation Sorting Join Operation Other Operations Evaluation of Expressions Introduction

More information

How to efficiently use the address register? Address register = contains the address of the operand to fetch from memory.

How to efficiently use the address register? Address register = contains the address of the operand to fetch from memory. Lesson 13 Storage Assignment Optimizations Sequence of accesses is very important Simple Offset Assignment This lesson will focus on: Code size and data segment size How to efficiently use the address

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

Intermediate Representation (IR)

Intermediate Representation (IR) Intermediate Representation (IR) Components and Design Goals for an IR IR encodes all knowledge the compiler has derived about source program. Simple compiler structure source code More typical compiler

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

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall CS 701 Charles N. Fischer Class Meets Tuesdays & Thursdays, 11:00 12:15 2321 Engineering Hall Fall 2003 Instructor http://www.cs.wisc.edu/~fischer/cs703.html Charles N. Fischer 5397 Computer Sciences Telephone:

More information

Compiler Construction 2009/2010: Intermediate Representation

Compiler Construction 2009/2010: Intermediate Representation Compiler Construction 2009/2010: Intermediate Representation Annette Bieniusa November 24, 2009 Outline 1 Contexts 2 Canonical Trees Using Expressions in different Contexts Compare the translation for

More information

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones. 6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

More information

Formal Languages and Compilers Lecture VI: Lexical Analysis

Formal Languages and Compilers Lecture VI: Lexical Analysis Formal Languages and Compilers Lecture VI: Lexical Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/ artale/ Formal

More information

Computer Architecture and Organization. Instruction Sets: Addressing Modes and Formats

Computer Architecture and Organization. Instruction Sets: Addressing Modes and Formats Computer Architecture and Organization Instruction Sets: Addressing Modes and Formats Addressing Modes Immediate Direct Indirect Register Register Indirect Displacement (Indexed) Stack Immediate Addressing

More information

Operational Semantics of Cool

Operational Semantics of Cool Operational Semantics of Cool Lecture 23 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 3 due on Friday Project 4 assigned on Friday. Due June 5, 11:55pm Office Hours this week Fri at 11am No Class

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

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