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

Size: px
Start display at page:

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

Transcription

1 Liveness Analysis and Register Allocation Xiao Jia May 3 rd,

2 Outline Control flow graph Liveness analysis Graph coloring Linear scan 2

3 Basic Block The code in a basic block has: one entry point, meaning no instruction is the destination of a jump instruction one exit point, meaning only the last instruction can cause the program to begin executing code in a different basic block. The code may be assembly code or some other sequence of instructions. 3

4 Basic Block (cont.) Formally, a sequence of instructions forms a basic block if: the instruction in each position dominates, or always executes before, all those in later positions, and no other instruction executes between two instructions in the sequence (e.g., no jump destination) 4

5 Generating Basic Blocks (1) Step 1. Identify the leaders in the code. Leaders are instructions which come under any of the following 3 categories : 1. The first instruction is a leader. 2. The target of a conditional or an unconditional goto/ jump instruction is a leader. 3. The instruction that immediately follows a conditional or an unconditional goto/jump instruction is a leader. Step 2. Starting from a leader, the set of all following instructions until and not including the next leader is the basic block corresponding to the starting leader. 5

6 Generating Basic Blocks (2) Instructions that end a basic block include Unconditional and conditional branches, both direct and indirect Returns to a calling procedure The return instruction itself. 6

7 Generating Basic Blocks (3) Instructions which begin a new basic block include Procedure and function entry points Targets of jumps or branches "Fall-through" instructions following some conditional branches 7

8 Control Flow Graph Nodes: basic blocks Edges (directed): jumps * Image from h,p:// 8

9 Liveness Analysis (read 9.2.5) The first value of X is dead (never used) The second value of X is live (may be used) X := 3 X := 4 Live = may be used in the future Y := X 9

10 Liveness Analysis A variable X is live at statement s if There exists a statement s that uses X There is a path from s to s That path has no intervening assignment to X 10

11 use[s] and def[s] gen[s] is the set of variables that are used in s before any assignment kill[s] is the set of variables that are assigned a value in s live-in[s] = use[s] (live-out[s] def[s]) live-out[final] = live-out[s] = p succ[s] live-in[p] 11

12 12

13 13

14 use[b] and def[b] gen[b] is the set of variables that are used in B before any assignment kill[b] is the set of variables that are assigned a value in B live-in[b] = use[b] (live-out[b] def[b]) live-out[final] = live-out[b] = S succ[b] live-in[s] 14

15 Iterative Algorithm 15

16 Questions? 16

17 Register Allocation Want to replace temporary variables with some fixed set of registers Within a single function Interprocedural register allocation is possible! (try to get bonus here) 17

18 We will judge this phase by 1. Code review 2. Limiting # of executed instructions, e.g. no greater than 1 million (1,000,000) Compare: only 160,000 for my optimized 8-queen 18

19 Two Approaches 1. Graph coloring 2. Linear scan 19

20 Graph coloring First: need to know which variables are live after each instruction Two simultaneously live variables cannot be allocated to the same register 20

21 Graph coloring For every node n in CFG, we have out[n] Set of temporaries live out of n Two variables interfere if Control Flow Graph both initially live (i.e. function arguments), or both appear in out[n] for any n, or one is defined and the other is in out[n] x = b - c where x is dead & b is live interfere How to assign registers to variables? 21

22 Interference graph Nodes of the graph = variables Edges connect variables that interfere with one another Nodes will be assigned a color corresponding to the register assigned to the variable Two colors can t be next to one another in the graph 22

23 Interference graph Instructions Live vars b = a + 2 c = b * b b = c + 1 return b * a 23

24 Interference graph Instructions Live vars b = a + 2 c = b * b b = c + 1 return b * a b,a 24

25 Interference graph Instructions Live vars b = a + 2 c = b * b b = c + 1 return b * a a,c b,a 25

26 Interference graph Instructions Live vars b = a + 2 c = b * b b = c + 1 return b * a b,a a,c b,a 26

27 Interference graph Instructions Live vars a b = a + 2 b,a c = b * b a,c b = c + 1 b,a return b * a 27

28 Interference graph color register Instructions Live vars a b = a + 2 a,b c = b * b a,c b = c + 1 a,b return b * a b a $t1 $t2 c 28

29 Interference graph color register Instructions Live vars a b = a + 2 a,b c = b * b a,c b = c + 1 a,b return b * a b a $t1 $t2 c 29

30 Graph coloring Questions: Can we efficiently find a coloring of the graph whenever possible? Can we efficiently find the optimum coloring of the graph? What do we do when there aren t enough colors (registers) to color the graph? 30

31 Coloring a graph Kempe s algorithm [1879] for finding a K- coloring of a graph Step 1 (simplify): find a node with at most K-1 edges and cut it out of the graph. (Remember this node on a stack for later stages.) 31

32 Coloring a graph Once a coloring is found for the simpler graph, we can always color the node we saved on the stack Step 2 (color): when the simplified subgraph has been colored, add back the node on the top of the stack and assign it a color not taken by one of the adjacent nodes 32

33 Coloring color register $t1 $t2 a stack: b c d e 33

34 Coloring color register $t1 $t2 a stack: b c d e c 34

35 Coloring color register $t1 $t2 a d b e c stack: e c 35

36 Coloring color register $t1 $t2 a d b e c stack: a e c 36

37 Coloring color register $t1 $t2 a d b e c stack: b a e c 37

38 Coloring color register $t1 $t2 a d b e c stack: d b a e c 38

39 Coloring color register $t1 $t2 a stack: d b e c b a e c 39

40 Coloring color register $t1 $t2 a d b e c stack: a e c 40

41 Coloring color register $t1 $t2 a stack: b c d e e c 41

42 Coloring color register $t1 $t2 a stack: b c d e c 42

43 Coloring color register $t1 $t2 a stack: b c d e 43

44 Failure If the graph cannot be colored, it will eventually be simplified to graph in which every node has at least K neighbors Sometimes, the graph is still K-colorable! Finding a K-coloring in all situations is an NP-complete problem We will have to approximate to make register allocators fast enough 44

45 Coloring color register $t1 $t2 a stack: b c d e 45

46 Coloring color register $t1 $t2 a b c stack: d d e all nodes have 2 neighbours! 46

47 Coloring color register $t1 $t2 a stack: b c d e b d 47

48 Coloring color register $t1 $t2 a d b e c stack: c e a b d 48

49 Coloring color register $t1 $t2 a stack: d b e c e a b d 49

50 Coloring color register $t1 $t2 a d b e c stack: a b d 50

51 Coloring color register $t1 $t2 a stack: b c d e b d 51

52 Coloring color register $t1 $t2 a stack: b c d e d 52

53 Coloring color register $t1 $t2 a stack: b c d e We got lucky! 53

54 Coloring color register $t1 $t2 Some graphs can t be colored in K colors: a stack: d b e c c b e a d 54

55 Coloring color register $t1 $t2 Some graphs can t be colored in K colors: a d b e c stack: b e a d 55

56 Coloring color register $t1 $t2 Some graphs can t be colored in K colors: a stack: b c d e e a d 56

57 Coloring color register $t1 $t2 Some graphs can t be colored in K colors: a stack: b c d e e a d no colors left for e! 57

58 Spilling Step 3 (spilling): once all nodes have K or more neighbors, pick a node for spilling Storage on the stack There are many heuristics that can be used to pick a node not in an inner loop 58

59 Spilling code We need to generate extra instructions to load variables from stack and store them These instructions use registers themselves. What to do? dedicated registers Stupid approach: always keep extra registers handy for shuffling data in and out: what a waste! Better approach:? 59

60 Spilling code We need to generate extra instructions to load variables from stack and store them These instructions use registers themselves. What to do? Stupid approach: always keep extra registers handy for shuffling data in and out: what a waste! Better approach: rewrite code introducing a new temporary; rerun liveness analysis and register allocation 60

61 Rewriting code Consider: add t1, t1, t2 Suppose t2 is selected for spilling and assigned to stack location 24($fp) Introduce a new temporary t3 for just this instruction and rewrite: ld t3, 24($fp) add t1, t1, t3 Advantage: t3 has a very short live range and is much less likely to interfere. Rerun the algorithm; fewer variables will spill 61

62 Precolored Nodes See Tiger book for more details Some variables are pre-assigned to registers Frame pointer Arguments ($a0, $a1, $a2, $a3) Function call defines (trashes) caller-save registers Treat these registers as special temporaries; before beginning, add them to the graph with their colors 62

63 Precolored Nodes See Tiger book for more details Can t simplify a graph by removing a precolored node Precolored nodes are the starting point of the coloring process Once simplified down to colored nodes start adding back the other nodes as before 63

64 Optimizing Moves Code generation produces a lot of extra move instructions mov t1, t2 If we can assign t1 and t2 to the same register, we do not have to execute the mov Idea: if t1 and t2 are not connected in the interference graph, we coalesce into a single variable 64

65 Coalescing Problem: coalescing can increase the number of interference edges and make a graph uncolorable coalesce t1 t2 t1/t2 Solution 1 (Briggs): avoid creation of high-degree (>= K) nodes Solution 2 (George): a can be coalesced with b if every neighbour t of a: already interferes with b, or has low-degree (< K) 65

66 Simplify & Coalesce Step 1 (simplify): simplify as much as possible without removing nodes that are the source or destination of a move (move-related nodes) Step 2 (coalesce): coalesce move-related nodes provided low-degree node results Step 3 (freeze): if neither steps 1 or 2 apply, freeze a move instruction: registers involved are marked not move-related and try step 1 again 66

67 Overall Algorithm Read papers for more variations Simplify, freeze and coalesce Liveness Mark possible spills Color & detect actual spills Rewrite code to implement actual spills 67

68 Questions? 68

69 Linear scan Given the live ranges of variables in a function, the algorithm scans all the live ranges in a single pass, allocating registers to variables in a greedy fashion. M. Poletto, V. Sarkar. Linear scan register allocation

70 70

71 71

72 72

73 Example Initially, active is empty # of available registers is R=2 73

74 Example Step 1: active=<a> # of available registers is R=2 74

75 Example Step 2: active=<a,b> # of available registers is R=2 75

76 Example Step 3: 3 live intervals overlap # of available registers is R=2 76

77 Example # of available registers is R=2 Step 3: 3 live intervals overlap spills C, whose interval ends furthest away from the current point active=<a,b> 77

78 Example Step 4: A expires active=<a,b,d> # of available registers is R=2 78

79 Example Step 5: B expires active=<b,d,e> # of available registers is R=2 79

80 Example In the end, C is the only variable not allocated to a register. # of available registers is R=2 80

81 Example In the end, C is the only variable not allocated to a register. # of available registers is R=2 Otherwise, both one of A and B and one of D and E would have been spilled to memory. 81

82 Questions? 82

83 Conclusion # of slides Graph coloring: ~50 Linear scan: ~10 Linear scan is much simpler! Only about 10% slower than a perfectly implemented graph coloring algorithm And your code may not be that perfect J Try to get bonus by comparing extensively the two approaches 83

84 Acknowledgements Wikipedia: basic block, control flow graph Graph coloring slides are adapted from Register Allocation by David Walker Linear scan pseudo-code and example are adapted from Linear Scan Register Allocation by M. Poletto and V. Sarkar. 84

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

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

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

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

CSc 553. Principles of Compilation. 23 : Register Allocation. Department of Computer Science University of Arizona

CSc 553. Principles of Compilation. 23 : Register Allocation. Department of Computer Science University of Arizona CSc 553 Principles of Compilation 3 : egister Allocation Department of Computer Science University of Arizona collberg@gmail.com Copyright c 0 Christian Collberg Introduction Lexing, Parsing Semantic Analysis,

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

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions Agenda CS738: Advanced Compiler Optimizations Data Flow Analysis Amey Karkare karkare@cse.iitk.ac.in http://www.cse.iitk.ac.in/~karkare/cs738 Department of CSE, IIT Kanpur Static analysis and compile-time

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

COSE312: Compilers. Lecture 20 Data-Flow Analysis (2)

COSE312: Compilers. Lecture 20 Data-Flow Analysis (2) COSE312: Compilers Lecture 20 Data-Flow Analysis (2) Hakjoo Oh 2017 Spring Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 1 / 18 Final Exam 6/19 (Mon), 15:30 16:45 (in class) Do not be late. Coverage:

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

Data Flow Information. already computed

Data Flow Information. already computed Data Flow Information Determine if Determine if a constant in loop modifies Determine if expression already computed Determine if not used later in program Data Flow Equations Local Information: Gen(B):

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

Today More register allocation Clarifications from last time Finish improvements on basic graph coloring concept Procedure calls Interprocedural

Today More register allocation Clarifications from last time Finish improvements on basic graph coloring concept Procedure calls Interprocedural More Register Allocation Last time Register allocation Global allocation via graph coloring Today More register allocation Clarifications from last time Finish improvements on basic graph coloring concept

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

Lecture 4 Introduction to Data Flow Analysis

Lecture 4 Introduction to Data Flow Analysis Lecture 4 Introduction to Data Flow Analysis I. Structure of data flow analysis II. Example 1: Reaching definition analysis III. Example 2: Liveness analysis IV. Generalization What is Data Flow Analysis?

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

ELEC 876: Software Reengineering

ELEC 876: Software Reengineering ELEC 876: Software Reengineering () Dr. Ying Zou Department of Electrical & Computer Engineering Queen s University Compiler and Interpreter Compiler Source Code Object Compile Execute Code Results data

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

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

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

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

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

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

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

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

Introduction. Data-flow Analysis by Iteration. CSc 553. Principles of Compilation. 28 : Optimization III

Introduction. Data-flow Analysis by Iteration. CSc 553. Principles of Compilation. 28 : Optimization III CSc 553 Principles of Compilation 28 : Optimization III Introduction Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg Computing Data-Flow Info.

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

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

Data Flow Analysis. Program Analysis

Data Flow Analysis. Program Analysis Program Analysis https://www.cse.iitb.ac.in/~karkare/cs618/ Data Flow Analysis Amey Karkare Dept of Computer Science and Engg IIT Kanpur Visiting IIT Bombay karkare@cse.iitk.ac.in karkare@cse.iitb.ac.in

More information

Why Global Dataflow Analysis?

Why Global Dataflow Analysis? Why Global Dataflow Analysis? Answer key questions at compile-time about the flow of values and other program properties over control-flow paths Compiler fundamentals What defs. of x reach a given use

More information

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

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

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

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

Register Allocation. Stanford University CS243 Winter 2006 Wei Li 1

Register Allocation. Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation Wei Li 1 Register Allocation Introduction Problem Formulation Algorithm 2 Register Allocation Goal Allocation of variables (pseudo-registers) in a procedure to hardware registers Directly

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

Lecture 2. Introduction to Data Flow Analysis

Lecture 2. Introduction to Data Flow Analysis Lecture 2 Introduction to Data Flow Analysis I II III Example: Reaching definition analysis Example: Liveness Analysis A General Framework (Theory in next lecture) Reading: Chapter 9.2 Advanced Compilers

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

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

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

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

Compiler Optimization and Code Generation

Compiler Optimization and Code Generation Compiler Optimization and Code Generation Professor: Sc.D., Professor Vazgen Melikyan 1 Course Overview Introduction: Overview of Optimizations 1 lecture Intermediate-Code Generation 2 lectures Machine-Independent

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

COP5621 Exam 4 - Spring 2005

COP5621 Exam 4 - Spring 2005 COP5621 Exam 4 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full credit

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

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

CS 6353 Compiler Construction, Homework #3

CS 6353 Compiler Construction, Homework #3 CS 6353 Compiler Construction, Homework #3 1. Consider the following attribute grammar for code generation regarding array references. (Note that the attribute grammar is the same as the one in the notes.

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 6 Foundations of Data Flow Analysis

Lecture 6 Foundations of Data Flow Analysis Review: Reaching Definitions Lecture 6 Foundations of Data Flow Analysis I. Meet operator II. Transfer functions III. Correctness, Precision, Convergence IV. Efficiency [ALSU 9.3] Phillip B. Gibbons 15-745:

More information

MIT Introduction to Dataflow Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT Introduction to Dataflow Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology MIT 6.035 Introduction to Dataflow Analysis Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Dataflow Analysis Used to determine properties of program that involve multiple

More information

Lecture 6 Foundations of Data Flow Analysis

Lecture 6 Foundations of Data Flow Analysis Lecture 6 Foundations of Data Flow Analysis I. Meet operator II. Transfer functions III. Correctness, Precision, Convergence IV. Efficiency ALSU 9.3 Phillip B. Gibbons 15-745: Foundations of Data Flow

More information

Principles of Compiler Design

Principles of Compiler Design Principles of Compiler Design Code Generation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Intermediate Code Code Generation Target Program

More information

Linear-Scan Register Allocation. CS 352 Lecture 12 11/28/07

Linear-Scan Register Allocation. CS 352 Lecture 12 11/28/07 Linear-Scan Register Allocation CS 352 Lecture 12 11/28/07 Introduction A simple local allocation algorithm Assume code is already scheduled Build a linear ordering of live ranges (also called live intervals

More information

CHAPTER 3. Register allocation

CHAPTER 3. Register allocation CHAPTER 3 Register allocation In chapter 1 we simplified the generation of x86 assembly by placing all variables on the stack. We can improve the performance of the generated code considerably if we instead

More information

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz Compiler Design Fall 2015 Data-Flow Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292 pedro@isi.edu

More information

Register Allocation, i. Overview & spilling

Register Allocation, i. Overview & spilling Register Allocation, i Overview & spilling 1 L1 p ::=(label f...) f ::=(label nat nat i...) i ::=(w

More information

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions. Available expressions Suppose we want to do common-subexpression elimination; that is, given a program that computes x y more than once, can we eliminate one of the duplicate computations? To find places

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

CHAPTER 3. Register allocation

CHAPTER 3. Register allocation CHAPTER 3 Register allocation In chapter 1 we simplified the generation of x86 assembly by placing all variables on the stack. We can improve the performance of the generated code considerably if we instead

More information

Introduction to Compiler Design: optimization and backend issues

Introduction to Compiler Design: optimization and backend issues Introduction to Compiler Design: optimization and backend issues Andy Pimentel group andy@science.uva.nl Introduction to Compiler Design A. Pimentel p. 1/127 Compilers: Organization Revisited Source Frontend

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

Linear Scan Register Allocation. Kevin Millikin

Linear Scan Register Allocation. Kevin Millikin Linear Scan Register Allocation Kevin Millikin Register Allocation Register Allocation An important compiler optimization Compiler: unbounded # of virtual registers Processor: bounded (small) # of registers

More information

Compilation Lecture 11a. Register Allocation Noam Rinetzky. Text book: Modern compiler implementation in C Andrew A.

Compilation Lecture 11a. Register Allocation Noam Rinetzky. Text book: Modern compiler implementation in C Andrew A. Compilation 0368-3133 Leture 11a Text book: Modern ompiler implementation in C Andrew A. Appel Register Alloation Noam Rinetzky 1 Registers Dediated memory loations that an be aessed quikly, an have omputations

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

Compiler Optimization Techniques Compiler Optimization Techniques Department of Computer Science, Faculty of ICT February 5, 2014 Introduction Code optimisations usually involve the replacement (transformation) of code from one sequence

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

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis Loop Invariant Code Motion Loop Invariant Code Motion Background: ud- and du-chains Last Time Control flow analysis Today Loop invariant code motion ud-chains A ud-chain connects a use of a variable to

More information

Data-flow Analysis - Part 2

Data-flow Analysis - Part 2 - Part 2 Department of Computer Science Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Data-flow analysis These are techniques that derive information about the flow of data

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

A main goal is to achieve a better performance. Code Optimization. Chapter 9

A main goal is to achieve a better performance. Code Optimization. Chapter 9 1 A main goal is to achieve a better performance Code Optimization Chapter 9 2 A main goal is to achieve a better performance source Code Front End Intermediate Code Code Gen target Code user Machineindependent

More information

Register Allocation, iii. Bringing in functions & using spilling & coalescing

Register Allocation, iii. Bringing in functions & using spilling & coalescing Register Allocation, iii Bringing in functions & using spilling & coalescing 1 Function Calls ;; f(x) = let y = g(x) ;; in h(y+x) + y*5 (:f (x

More information

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Data-flow analysis These are techniques that derive information about the flow

More information

The C2 Register Allocator. Niclas Adlertz

The C2 Register Allocator. Niclas Adlertz The C2 Register Allocator Niclas Adlertz 1 1 Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated

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

CS2210: Compiler Construction. Compiler Optimization

CS2210: Compiler Construction. Compiler Optimization Overview of Optimizations Goal of optimization is to generate better code Impossible to generate optimal code Factors beyond control of compiler (user input, OS design, HW design) all affect what is optimal

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

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts Compiler Structure Source Code Abstract Syntax Tree Control Flow Graph Object Code CMSC 631 Program Analysis and Understanding Fall 2003 Data Flow Analysis Source code parsed to produce AST AST transformed

More information

Code generation and local optimization

Code generation and local optimization Code generation and local optimization Generating assembly How do we convert from three-address code to assembly? Seems easy! But easy solutions may not be the best option What we will cover: Instruction

More information

Code generation and local optimization

Code generation and local optimization Code generation and local optimization Generating assembly How do we convert from three-address code to assembly? Seems easy! But easy solutions may not be the best option What we will cover: Instruction

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

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

Induction Variable Identification (cont)

Induction Variable Identification (cont) Loop Invariant Code Motion Last Time Uses of SSA: reaching constants, dead-code elimination, induction variable identification Today Finish up induction variable identification Loop invariant code motion

More information

What Compilers Can and Cannot Do. Saman Amarasinghe Fall 2009

What Compilers Can and Cannot Do. Saman Amarasinghe Fall 2009 What Compilers Can and Cannot Do Saman Amarasinghe Fall 009 Optimization Continuum Many examples across the compilation pipeline Static Dynamic Program Compiler Linker Loader Runtime System Optimization

More information

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18 General issues Section 9.1 Target language: absolute machine language all addresses refer to actual addresses program placed in a fixed location in memory relocatable machine language (object modules)

More information

Code generation and optimization

Code generation and optimization Code generation and timization Generating assembly How do we convert from three-address code to assembly? Seems easy! But easy solutions may not be the best tion What we will cover: Peephole timizations

More information

Travelling Salesman Problem: Tabu Search

Travelling Salesman Problem: Tabu Search Travelling Salesman Problem: Tabu Search (Anonymized) April 2017 Abstract The Tabu Search algorithm is a heuristic method to find optimal solutions to the Travelling Salesman Problem (TSP). It is a local

More information

Compilation 2013 Liveness Analysis

Compilation 2013 Liveness Analysis Compilation 2013 Liveness Analysis Erik Ernst Aarhus University For good complexity management, we have used an unbounded number of registers (temporaries) To save resources, many of them can be merged

More information

Compiler Design Spring 2017

Compiler Design Spring 2017 Compiler Design Spring 2017 8.6 Live variables Dr. Zoltán Majó Compiler Group Java HotSpot Virtual Machine Oracle Corporation Live variables Foundations for several optimizations If a variable is not live,

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

CS 352 Compilers: Principles and Practice Final Examination, 12/11/05

CS 352 Compilers: Principles and Practice Final Examination, 12/11/05 CS 352 Compilers: Principles and Practice Final Examination, 12/11/05 Instructions: Read carefully through the whole exam first and plan your time. Note the relative weight of each question and part (as

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

Review; questions Basic Analyses (2) Assign (see Schedule for links)

Review; questions Basic Analyses (2) Assign (see Schedule for links) Class 2 Review; questions Basic Analyses (2) Assign (see Schedule for links) Representation and Analysis of Software (Sections -5) Additional reading: depth-first presentation, data-flow analysis, etc.

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

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

CS202 Compiler Construction

CS202 Compiler Construction S202 ompiler onstruction pril 15, 2003 S 202-32 1 ssignment 11 (last programming assignment) Loop optimizations S 202-32 today 2 ssignment 11 Final (necessary) step in compilation! Implement register allocation

More information