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

Similar documents
Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

COMP 210 Example Question Exam 2 (Solutions at the bottom)

X86 Stack Calling Function POV

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Machine-level Programming (3)

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Machine Program: Procedure. Zhaoguo Wang

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

W4118: PC Hardware and x86. Junfeng Yang

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Processes (Intro) Yannis Smaragdakis, U. Athens

CPEG421/621 Tutorial

CSE 351: Week 4. Tom Bergan, TA

CS213. Machine-Level Programming III: Procedures

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Instruction Set Architecture

Subprograms: Local Variables

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

x86 assembly CS449 Fall 2017

Machine-Level Programming III: Procedures

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

CS429: Computer Organization and Architecture

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CPSC W Term 2 Problem Set #3 - Solution

Lecture 4 CIS 341: COMPILERS

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

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

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

211: Computer Architecture Summer 2016

Instruction Set Architecture

Instruction Set Architecture

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

An Elegant Weapon for a More Civilized Age

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Systems I. Machine-Level Programming V: Procedures

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

16.317: Microprocessor Systems Design I Fall 2015

CS61 Section Solutions 3

x86 assembly CS449 Spring 2016

X86 Assembly -Procedure II:1

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

Chapter 4! Processor Architecture!

Program Exploitation Intro

CS , Fall 2004 Exam 1

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker

Digital Forensics Lecture 3 - Reverse Engineering

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

Assignment 11: functions, calling conventions, and the stack

Sungkyunkwan University

CS642: Computer Security

CPS104 Recitation: Assembly Programming

cmovxx ra, rb 2 fn ra rb irmovl V, rb rb V rmmovl ra, D(rB) 4 0 ra rb D mrmovl D(rB), ra 5 0 ra rb D OPl ra, rb 6 fn ra rb jxx Dest 7 fn Dest

Chapter 11. Addressing Modes

3. Process Management in xv6

Sungkyunkwan University

Function Call Convention

CS241 Computer Organization Spring 2015 IA

Instruction Set Architecture

CISC 360 Instruction Set Architecture

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

CS3330 Exam 1 Spring Name: CS3330 Spring 2015 Exam 1 Page 1 of 6 ID: KEY

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

x86 Assembly Tutorial COS 318: Fall 2017

CS61, Fall 2012 Midterm Review Section

Register Allocation, i. Overview & spilling

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

Assembly Language Lab # 9

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

An Experience Like No Other. Stack Discipline Aug. 30, 2006

Ausgewählte Betriebssysteme. Anatomy of a system call

Stack Discipline Jan. 19, 2018

You may work with a partner on this quiz; both of you must submit your answers.

ANITA S SUPER AWESOME RECITATION SLIDES

BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

16.317: Microprocessor Systems Design I Fall 2014

Lab 10: Introduction to x86 Assembly

Transcription:

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 <- eax) ;; save our argument (call :g) ;; call g with our argument (y <- eax) ;; save g's result in y (eax += x) ;; compute h's arg (call :h) ;; call h (y5 <- y) ;; compute y*5 in y5, i (y5 *= 5) ;; compute y*5 in y5, ii (eax += y5) ;; add h's res to y*5 (return)) ;; and we're done. 2

Gen & Kill gen kill 1: :f () () 2: (x <- eax) (eax) (x) 3: (call :g) (eax ecx edx) (ebx ecx edx eax) 4: (y <- eax) (eax) (y) 5: (eax += x) (eax x) (eax) 6: (call :h) (eax ecx edx) (ebx ecx edx eax) 7: (y5 <- y) (y) (y5) 8: (y5 *= 5) (y5) (y5) 9: (eax += y5) (eax y5) (eax) 10: (return) (eax edi esi) () 3

Liveness in 1: :f () () 2: (x <- eax) () () 3: (call :g) () () 4: (y <- eax) () () 5: (eax += x) () () 6: (call :h) () () 7: (y5 <- y) () () 8: (y5 *= 5) () () 9: (eax += y5) () () 10: (return) () () out 4

Liveness in 1: :f () () 2: (x <- eax) (eax) () 3: (call :g) (eax ecx edx) () 4: (y <- eax) (eax) () 5: (eax += x) (eax x) () 6: (call :h) (eax ecx edx) () 7: (y5 <- y) (y) () 8: (y5 *= 5) (y5) () 9: (eax += y5) (eax y5) () 10: (return) (eax edi esi) () out 5

Liveness in out 1: :f () (eax) 2: (x <- eax) (eax) (eax ecx edx) 3: (call :g) (eax ecx edx) (eax) 4: (y <- eax) (eax) (eax x) 5: (eax += x) (eax x) (eax ecx edx) 6: (call :h) (eax ecx edx) (y) 7: (y5 <- y) (y) (y5) 8: (y5 *= 5) (y5) (eax y5) 9: (eax += y5) (eax y5) (eax edi esi) 10: (return) (eax edi esi) () 6

Liveness in out 1: :f (eax) (eax) 2: (x <- eax) (eax ecx edx) (eax ecx edx) 3: (call :g) (eax ecx edx) (eax) 4: (y <- eax) (eax x) (eax x) 5: (eax += x) (eax ecx edx x) (eax ecx edx) 6: (call :h) (eax ecx edx y) (y) 7: (y5 <- y) (y) (y5) 8: (y5 *= 5) (eax y5) (eax y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 7

Liveness in out 1: :f (eax) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx) 3: (call :g) (eax ecx edx) (eax x) 4: (y <- eax) (eax x) (eax ecx edx x) 5: (eax += x) (eax ecx edx x) (eax ecx edx y) 6: (call :h) (eax ecx edx y) (y) 7: (y5 <- y) (y) (eax y5) 8: (y5 *= 5) (eax y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 8

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx) 3: (call :g) (eax ecx edx x) (eax x) 4: (y <- eax) (eax ecx edx x) (eax ecx edx x) 5: (eax += x) (eax ecx edx x y) (eax ecx edx y) 6: (call :h) (eax ecx edx y) (y) 7: (y5 <- y) (eax y) (eax y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 9

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edx x) (eax ecx edx x) 4: (y <- eax) (eax ecx edx x) (eax ecx edx x y) 5: (eax += x) (eax ecx edx x y) (eax ecx edx y) 6: (call :h) (eax ecx edx y) (eax y) 7: (y5 <- y) (eax y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 10

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edx x) (eax ecx edx x) 4: (y <- eax) (eax ecx edx x) (eax ecx edx x y) 5: (eax += x) (eax ecx edx x y) (eax ecx edx y) 6: (call :h) (eax ecx edx y) (eax y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 11

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edx x) (eax ecx edx x) 4: (y <- eax) (eax ecx edx x) (eax ecx edx x y) 5: (eax += x) (eax ecx edx x y) (eax ecx edx y) 6: (call :h) (eax ecx edx y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 12

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edx x) (eax ecx edx x) 4: (y <- eax) (eax ecx edx x) (eax ecx edx x y) 5: (eax += x) (eax ecx edx x y) (eax ecx edx y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 13

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edx x) (eax ecx edx x) 4: (y <- eax) (eax ecx edx x) (eax ecx edx x y) 5: (eax += x) (eax ecx edx x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 14

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edx x) (eax ecx edx x) 4: (y <- eax) (eax ecx edx x) (eax ecx edx x y) 5: (eax += x) (eax ecx edi edx esi x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 15

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edx x) (eax ecx edx x) 4: (y <- eax) (eax ecx edx x) (eax ecx edi edx esi x y) 5: (eax += x) (eax ecx edi edx esi x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 16

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edx x) (eax ecx edx x) 4: (y <- eax) (eax ecx edi edx esi x) (eax ecx edi edx esi x y) 5: (eax += x) (eax ecx edi edx esi x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 17

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edx x) (eax ecx edi edx esi x) 4: (y <- eax) (eax ecx edi edx esi x) (eax ecx edi edx esi x y) 5: (eax += x) (eax ecx edi edx esi x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 18

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edx x) 3: (call :g) (eax ecx edi edx esi x) (eax ecx edi edx esi x) 4: (y <- eax) (eax ecx edi edx esi x) (eax ecx edi edx esi x y) 5: (eax += x) (eax ecx edi edx esi x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 19

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edx) (eax ecx edi edx esi x) 3: (call :g) (eax ecx edi edx esi x) (eax ecx edi edx esi x) 4: (y <- eax) (eax ecx edi edx esi x) (eax ecx edi edx esi x y) 5: (eax += x) (eax ecx edi edx esi x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 20

Liveness in out 1: :f (eax ecx edx) (eax ecx edx) 2: (x <- eax) (eax ecx edi edx esi) (eax ecx edi edx esi x) 3: (call :g) (eax ecx edi edx esi x) (eax ecx edi edx esi x) 4: (y <- eax) (eax ecx edi edx esi x) (eax ecx edi edx esi x y) 5: (eax += x) (eax ecx edi edx esi x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 21

Liveness in out 1: :f (eax ecx edx) (eax ecx edi edx esi) 2: (x <- eax) (eax ecx edi edx esi) (eax ecx edi edx esi x) 3: (call :g) (eax ecx edi edx esi x) (eax ecx edi edx esi x) 4: (y <- eax) (eax ecx edi edx esi x) (eax ecx edi edx esi x y) 5: (eax += x) (eax ecx edi edx esi x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 22

Liveness in out 1: :f (eax ecx edi edx esi) (eax ecx edi edx esi) 2: (x <- eax) (eax ecx edi edx esi) (eax ecx edi edx esi x) 3: (call :g) (eax ecx edi edx esi x) (eax ecx edi edx esi x) 4: (y <- eax) (eax ecx edi edx esi x) (eax ecx edi edx esi x y) 5: (eax += x) (eax ecx edi edx esi x y) (eax ecx edi edx esi y) 6: (call :h) (eax ecx edi edx esi y) (eax edi esi y) 7: (y5 <- y) (eax edi esi y) (eax edi esi y5) 8: (y5 *= 5) (eax edi esi y5) (eax edi esi y5) 9: (eax += y5) (eax edi esi y5) (eax edi esi) 10: (return) (eax edi esi) () 23

Liveness :f (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (return) x y y5 eax ebx ecx edi edx esi 24

ebx eax x ecx esi edx edi y y5 25

ebx eax x ecx esi edx y y5 edi y5 26

ebx eax x ecx esi edx y y5 edi x y5 27

ebx eax x ecx esi edx y y y5 edi x y5 28

ebx eax x ecx esi edx y y y5 edi x y5 29

ebx eax x ecx esi edx y y5 edi x y5 30

ebx eax x ecx esi edx y y5 edi x y5 31

ebx eax x ecx esi edx y y5 edi y5 32

ebx eax x ecx esi edx y y5 edi y5 33

ebx eax x ecx esi edx edi y y5 34

ebx eax x ecx esi edx edi y y5 35

Spilling y Before: :f (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (return) After: :f (x <- eax) (call :g) ((mem ebp -4) <- eax) (eax += x) (call :h) (y5 <- (mem ebp -4)) (y5 *= 5) (eax += y5) (return) 36

Spilling y :f (x <- eax) (call :g) ((mem ebp -4) <- eax) (eax += x) (call :h) (y5 <- (mem ebp -4)) (y5 *= 5) (eax += y5) (return) ebp x y5 eax ebx ecx edi edx esi 37

eax ebx x ecx esi edx edi y5 38

eax ebx x ecx esi edx edi y5 y5 39

eax ebx x ecx esi edx edi x y5 y5 40

eax ebx x ecx esi edx edi x y5 y5 41

eax ebx x ecx esi edx edi y5 y5 42

eax ebx x ecx esi edx edi y5 y5 43

eax ebx x ecx esi edx edi y5 44

eax ebx x ecx esi edx edi y5 45

Spilling x Before: :f (x <- eax) (call :g) ((mem ebp -4) <- eax) (eax += x) (call :h) (y5 <- (mem ebp -4)) (y5 *= 5) (eax += y5) (return) After: :f ((mem ebp -8) <- eax) (call :g) ((mem ebp -4) <- eax) (sx0 <- (mem ebp -8)) (eax += sx0) (call :h) (y5 <- (mem ebp -4)) (y5 *= 5) (eax += y5) (return) Note that this time we introduce a sx0, but compare its live range to x s 46

Spilling x :f ((mem ebp -8) <- eax) (call :g) ((mem ebp -4) <- eax) (sx0 <- (mem ebp -8)) (eax += sx0) (call :h) (y5 <- (mem ebp -4)) (y5 *= 5) (eax += y5) (return) ebp sx0 y5 eax ebx ecx edi edx esi 47

ebx ecx eax edx esi sx0 edi y5 48

ebx ecx eax edx esi sx0 edi sx0 y5 49

ebx ecx eax edx esi sx0 edi y5 sx0 y5 50

ebx ecx eax edx esi sx0 edi y5 sx0 y5 51

ebx ecx eax edx esi sx0 edi sx0 y5 52

ebx ecx eax edx esi sx0 edi sx0 y5 53

ebx ecx eax edx esi sx0 edi y5 54

ebx ecx eax edx esi sx0 edi y5 55

Live ranges :f (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (return) x y y5 eax ebx ecx edi edx esi 56

Live ranges We spilled two variables that have relatively short live ranges, but look at those long live ranges with no uses of the variables that the callee save registers, i.e. edi and esi, have. We d rather spill them. 57

Spilling callee saves Unfortunately, it gets complicated to spill real registers. Instead, a trick: we just make up new variables to hold their values. Semantics of the program does not change, but: Now the real registers now have short live ranges, and New temporaries are spillable 58

Adding new variables g-ans Before: :f (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (return) After: :f (z1 <- edi) (z2 <- esi) (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (edi <- z1) (esi <- z2) (return) Init new variables at beginning of fun, restore them before returning or making a tail call. 59

Live ranges with new variables :f (z1 <- edi) (z2 <- esi) (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (edi <- z1) (esi <- z2) (return) x y y5 z1 z2 eax ebx ecx edi edx esi 60

Spilling z1 & z2 Before: :f (z1 <- edi) (z2 <- esi) (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (edi <- z1) (esi <- z2) (return) After: :f ((mem ebp -4) <- edi) ((mem ebp -8) <- esi) (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (edi <- (mem ebp -4)) (esi <- (mem ebp -8)) (return) 61

x esi edi eax y5 ebx y ecx edx 62

x esi edi eax y5 ebx ecx edx y y 63

x esi edi eax y5 ebx ecx edx y x y 64

x esi edi eax y5 ebx y5 ecx edx y x y 65

x esi edi eax y5 ebx y5 ecx edx y x y 66

x esi edi eax y5 ebx ecx edx y x y 67

x esi edi eax y5 ebx ecx edx y x y 68

x esi edi eax y5 ebx ecx edx y y 69

x esi edi eax y5 ebx ecx edx y y 70

x esi edi eax y5 ebx y ecx edx 71

x esi edi eax y5 ebx y ecx edx 72

How to choose spills Pick variables with long live ranges and few uses to spill (callee saves have this profile) In this case, we can spill z1 and z2 to save us from spilling variables x and y that are frequently accessed 73

Finishing up register allocation When you ve spilled enough to successfully allocate the program, count the number of spills and adjust esp at the beginning of the function. (Recall calling convention.) 74

Calling convention reminder (see lecture03.txt) (call s) pushl $<new-label> pushl %ebp movl %esp, %ebp jmp <s> <new-label>: (return) movl %ebp, %esp popl %ebp ret // pop & goto (tail-call s) movl %ebp, %esp jmp <s> (cleaned up version of call followed by return) 75

Registers: allocated (:f (esp -= 8) ((mem ebp -4) <- esi) ((mem ebp -8) <- edi) (esi <- eax) (call :g) (edi <- eax) (eax += esi) (call :h) (ebx <- edi) (ebx *= 5) (eax += ebx) (esi <- (mem ebp -4)) (edi <- (mem ebp -8)) (return)) 76

Coalescing If we see a (x <- y) instruction, we might be able to just change all of the x's into y's That s called coalescing x and y Lets use coalescing to remove y and y5 from our example program 77

Coalescing example :f ((mem ebp -4) <- edi) ((mem ebp -8) <- esi) (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (edi <- (mem ebp -4)) (esi <- (mem ebp -8)) (return) :f ((mem ebp -4) <- edi) ((mem ebp -8) <- esi) (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y <- y) (y *= 5) (eax += y) (edi <- (mem ebp -4)) (esi <- (mem ebp -8)) (return) 78

Live ranges before coalescing :f ((mem ebp -4) <- edi) ((mem ebp -8) <- esi) (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y5 <- y) (y5 *= 5) (eax += y5) (edi <- (mem ebp -4)) (esi <- (mem ebp -8)) (return) ebp x y y5 eax ebx ecx edi edx esi 79

Live ranges after coalescing :f ((mem ebp -4) <- edi) ((mem ebp -8) <- esi) (x <- eax) (call :g) (y <- eax) (eax += x) (call :h) (y <- y) (y *= 5) (eax += y) (edi <- (mem ebp -4)) (esi <- (mem ebp -8)) (return) ebp x y eax ebx ecx edi edx esi 80

Lets try to register allocate the coalesced graph 81

esi x edi eax y ebx edx ecx 82

esi x edi eax y ebx edx y ecx 83

esi x edi eax y ebx edx x y ecx 84

esi x edi eax y ebx edx x y ecx 85

esi x edi eax y ebx edx y ecx 86

esi x edi eax y ebx edx y ecx 87

esi x edi eax y ebx edx ecx 88

esi x edi eax y ebx edx ecx 89

It worked that time, but this doesn t always work 90

Coalescing Problem (r_a <- 1) (r_z <- r_a) (r_1 <- 1) (r_2 <- 2) (r_3 <- 3) (r_a += 1) (r_4 <- 4) (r_5 <- 5) ( <- 6) (r_1 += 1) (r_2 += 1) (r_3 += 1) (r_z <- 1) (r_4 += 1) (r_5 += 1) ( += 1) (r_z += 1) r_1 r_2 r_3 r_4 r_5 r_a r_z eax ebx ecx edi edx esi 91

r_z r_1 r_a r_2 r_3 r_5 r_4 92

r_z r_1 r_a r_2 r_3 r_5 r_z r_4 93

r_z r_1 r_a r_2 r_3 r_5 r_a r_z r_4 94

r_z r_1 r_a r_2 r_3 r_5 r_a r_z r_4 95

r_z r_1 r_a r_2 r_3 r_5 r_5 r_a r_z r_4 96

r_z r_1 r_a r_2 r_3 r_5 r_4 r_5 r_a r_z r_4 97

r_z r_1 r_a r_2 r_3 r_5 r_3 r_4 r_5 r_a r_z r_4 98

r_z r_2 r_1 r_3 r_a r_5 r_2 r_3 r_4 r_5 r_a r_z r_4 99

r_z r_2 r_1 r_3 r_a r_5 r_1 r_2 r_3 r_4 r_5 r_a r_z r_4 100

r_z r_2 r_1 r_3 r_a r_5 r_2 r_3 r_4 r_5 r_a r_z r_4 101

r_z r_2 r_1 r_3 r_a r_5 r_2 r_3 r_4 r_5 r_a r_z r_4 102

r_z r_1 r_a r_2 r_3 r_5 r_3 r_4 r_5 r_a r_z r_4 103

r_z r_1 r_a r_2 r_3 r_5 r_3 r_4 r_5 r_a r_z r_4 104

r_z r_1 r_a r_2 r_3 r_5 r_4 r_5 r_a r_z r_4 105

r_z r_1 r_a r_2 r_3 r_5 r_4 r_5 r_a r_z r_4 106

r_z r_1 r_a r_2 r_3 r_5 r_5 r_a r_z r_4 107

r_z r_1 r_a r_2 r_3 r_5 r_5 r_a r_z r_4 108

r_z r_1 r_a r_2 r_3 r_5 r_a r_z r_4 109

r_z r_1 r_a r_2 r_3 r_5 r_a r_z r_4 110

r_z r_1 r_a r_2 r_3 r_5 r_a r_z r_4 111

r_z r_1 r_a r_2 r_3 r_5 r_a r_z r_4 112

r_z r_1 r_a r_2 r_3 r_5 r_z r_4 113

r_z r_1 r_a r_2 r_3 r_5 r_z r_4 114

r_z r_1 r_a r_2 r_3 r_5 r_4 115

r_z r_1 r_a r_2 r_3 r_5 r_4 116

Now, we coalesce r a and r z 117

Coalescing Problem (r_a <- 1) (r_z <- r_a) (r_1 <- 1) (r_2 <- 2) (r_3 <- 3) (r_a += 1) (r_4 <- 4) (r_5 <- 5) ( <- 6) (r_1 += 1) (r_2 += 1) (r_3 += 1) (r_z <- 1) (r_4 += 1) (r_5 += 1) ( += 1) (r_z += 1) r_1 r_2 r_3 r_4 r_5 r_a r_z eax ebx ecx edi edx esi 118

Coalescing Problem (r_z <- 1) (r_z <- r_z) (r_1 <- 1) (r_2 <- 2) (r_3 <- 3) (r_z += 1) (r_4 <- 4) (r_5 <- 5) ( <- 6) (r_1 += 1) (r_2 += 1) (r_3 += 1) (r_z <- 1) (r_4 += 1) (r_5 += 1) ( += 1) (r_z += 1) r_1 r_2 r_3 r_4 r_5 r_z eax ebx ecx edi edx esi 119

r_1 r_z r_2 r_3 r_4 r_5 120

r_1 r_z r_2 r_3 r_z r_4 r_5 121

r_1 r_z r_2 r_3 r_z r_5 r_4 122

r_1 r_z r_2 r_3 r_5 r_z r_5 r_4 123

r_1 r_z r_2 r_3 r_4 r_5 r_z r_5 r_4 124

r_1 r_z r_2 r_3 r_3 r_4 r_5 r_z r_5 r_4 125

r_1 r_z r_2 r_3 r_2 r_3 r_4 r_5 r_z r_5 r_4 126

r_1 r_z r_2 r_3 r_1 r_2 r_3 r_4 r_5 r_z r_5 r_4 127

r_1 r_z r_2 r_3 r_2 r_3 r_4 r_5 r_z r_5 r_4 128

r_1 r_z r_2 r_3 r_2 r_3 r_4 r_5 r_z r_5 r_4 129

r_1 r_z r_2 r_3 r_3 r_4 r_5 r_z r_5 r_4 130

r_1 r_z r_2 r_3 r_3 r_4 r_5 r_z r_5 r_4 131

r_1 r_z r_2 r_3 r_4 r_5 r_z r_5 r_4 132

r_1 r_z r_2 r_3 r_4 r_5 r_z r_5 r_4 133

r_1 r_z r_2 r_3 r_5 r_z r_5 r_4 134

r_1 r_z r_2 r_3 r_5 r_z r_5 r_4 135

r_1 r_z r_2 r_3 r_z r_5 r_4 136

r_1 r_z r_2 r_3 r_z r_5 r_4 137

r_1 r_z r_2 r_3 r_z r_4 r_5 138

r_1 r_z r_2 r_3 r_z r_4 r_5 139

r_1 r_z r_2 r_3 r_4 r_5 140

r_1 r_z r_2 r_3 r_4 r_5 141

Coalescing during graph coloring Extend the interference graph with a new kind of edge, called a move edge. A move edge connects two nodes if there is a (x <- y) instruction in the program Combine two move-edge connected nodes into a single node (at any step in the coloring algorithm), if: They don t interfere The resulting node has fewer than 6 neighbors This ensures the graph is colorable (if it was before) 142

Roadmap: putting it all together 143

Programming There are a number of different modules to put together A liveness library: gen & kill functions on instructions; the in and out loop; going from liveness to interference A graph library: creating graphs, creating nodes, creating edges, removing nodes (and their edges), iterating over edges and nodes An interference library: build a graph from the liveness information 144

Programming There are a number of different modules to put together, cotd A coloring library: color a graph using the coloring algorithm A spilling library: given a variable and a stack position, rewrite the program to move the variable in and out of the stack right as it is used The final translation: When you have a valid coloring, rewrite the variables to use registers and insert the esp adjustment, turning the L2 program into an L1 one. 145

Test 146

Unit testing The most underrated part of developing good software is testing it well. Build simple (unit) tests for the api for each module, as you design the API As you write the code, write a test for each different case in the code together with the case itself Whenever you find a bug, always add a test case before fixing the bug; make sure the test case fails so you know you wrote it properly 147