CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Similar documents
CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE 504: Compiler Design. Code Generation

Compiler Optimization Techniques

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Code Generation (#')! *+%,-,.)" !"#$%! &$' (#')! 20"3)% +"#3"0- /)$)"0%#"

Principles of Compiler Design

Code generation and local optimization

Code generation and local optimization

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CODE GENERATION Monday, May 31, 2010

Code generation and optimization

Code Generation. M.B.Chandak Lecture notes on Language Processing

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

UNIT-V. Symbol Table & Run-Time Environments Symbol Table

CSc 453. Compilers and Systems Software. 21 : Code Generation II. Department of Computer Science University of Arizona

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

tokens parser 1. postfix notation, 2. graphical representation (such as syntax tree or dag), 3. three address code

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

More Code Generation and Optimization. Pat Morin COMP 3002

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

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

OO software systems are systems of interacting objects.

Figure 28.1 Position of the Code generator

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

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

Compiler Optimization and Code Generation

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

Instruction Selection and Scheduling

CS415 Compilers. Instruction Scheduling and Lexical Analysis

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

Lecture 25: Register Allocation

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Compiler Design and Construction Optimization

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

CSE 490/590 Computer Architecture Homework 2

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

CSc 453. Code Generation I Christian Collberg. Compiler Phases. Code Generation Issues. Slide Compilers and Systems Software.

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

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

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

Register Allocation & Liveness Analysis

Computer Architecture Prof. Mainak Chaudhuri Department of Computer Science & Engineering Indian Institute of Technology, Kanpur

Architectures. Code Generation Issues III. Code Generation Issues II. Machine Architectures I

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE 115 / 503 INTRODUCTION TO COMPUTER SCIENCE I. Dr. Carl Alphonce Dr. Jesse Hartloff

Topic 12: Register Allocation

Fundamentals of Programming Session 13

6.823 Computer System Architecture

A weekly Spa Day for your Computer

Instruction Scheduling

ECE 486/586. Computer Architecture. Lecture # 7

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

Lecture 20: AVR Programming, Continued. AVR Program Visible State (ones we care about for now)

Compiler Design Spring 2018

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Register Allocation (wrapup) & Code Scheduling. Constructing and Representing the Interference Graph. Adjacency List CS2210

EECS 583 Class 8 Classic Optimization

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Chapter 2: Instructions:

Lecture 3 Local Optimizations, Intro to SSA

Topic Notes: MIPS Instruction Set Architecture

Introduction. CSc 453. Compilers and Systems Software. 19 : Code Generation I. Department of Computer Science University of Arizona.

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

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

CPSC 213. Introduction to Computer Systems. Scalars and Arrays. Unit 1b

Lecture 21 CIS 341: COMPILERS

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

CS 406/534 Compiler Construction Putting It All Together

CS5363 Final Review. cs5363 1

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

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

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Register Allocation via Hierarchical Graph Coloring

MIPS History. ISA MIPS Registers

Levels of Programming. Registers

CSE 401/M501 Compilers

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

Compilers and Code Optimization EDOARDO FUSELLA

COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013

P423/P523 Compilers Register Allocation

CSE410 aka CSE306 Software Quality

CS293S Redundancy Removal. Yufei Ding

Calvin Lin The University of Texas at Austin

MICROPROGRAMMED CONTROL

COMPILER DESIGN - CODE OPTIMIZATION

Transcription:

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall

Phases of a compiler Target machine code generation Figure 1.6, page 5 of text

B1 i = 1 B2 j = 1 B3 t1 = 10 * i t2 = t1 + j t3 = 8 * t2 t4 = t3-88 a[t4] = 0.0 j = j + 1 if j<= 10 goto B3 B4 i = i + 1 if i <= 10 goto B2 B5 i = 1 B6 t5 = i - 1 t6 = 88 * t5 a[t6] = 1.0 i = i + 1 if i <= 10 goto B6 ENTRY Flow Graph Figure 8.9 [p. 530] Entry and exit nodes added. Jump targets replaced by block names. EXIT

DAG for basic block Example 8.10 [p. 534] 1) a = b + c 2) b = a - d 3) c = b + c 4) d = a - d

Example 8.10 [p. 534] Apply the "value-number" method from section 6.1.1 + c 1) a = b + c 2) b = a - d - b,d 3) c = b + c + a d 0 4) d = a - d b 0 c 0

Example 8.10 [p. 534] 1) a = b + c 2) b = a - d - + b,d c 3) c = b + c 4) d = b + a d 0 b 0 c 0

8.6 A Simple Code Generator [p. 542] algorithm focuses on generation of code for a single basic block generates code for each three address code instruction manages register allocations/ assignment to avoid redundant loads/stores

register descriptor "For each available register, a register descriptor keeps track of the variables names whose current value is in that register." [p. 543]

address descriptor "For each program variable, an address descriptor keeps track of the location or locations where the current value of that variable can be found." [p. 543]

getreg function " getreg(i) selects registers for each memory location associated with the three-address instruction I." [p. 544]

copy instructions x = y "We assume getreg will always choose the same register for both x and y. If y is not already in that register Ry, then generate the machine instruction LD Ry, y. If y was already in Ry, we do nothing. It is only necessary that we adjust the register descriptor for Ry so that it includes x as one of the values found there." [p. 544]

Writing back to memory at end of block At the end of a basic block we must ensure that live variables are stored back into memory. " for each variable x whose address descriptor does not say that is value is located in the memory location for x, we must generate the instruction ST x, R, where R is a register in which x's value exists at the end of the block." [p. 545]

Updating register descriptors (RD) and address descriptors (AD) 1. LD R, x (a) Set RD of R to only x (b) Add R to AD of x 2. ST x, R (a) Add &x to AD of x 3. OP Rx, Ry, Rz for x = y op z (a) Set RD of Rx to only x (b) Set AD of x to only Rx (&x not in AD of x!) (c) Remove Rx from the AD of any variable other than x 4. "When we process a copy statement x = y, after generating the load for y into register Ry, if needed, and after managing descriptors as for all load statement (per rule 1):" [p. 545] (a) Add x to the RD of Ry (b) Set AD of x to only Ry

Example [p. 546] t = a - b u = a - c v = t + u a = d d = v + u

R1 R2 R3 a b c d

R1 R2 R3 a b c d t = a - b LD R1, a LD R2, b SUB R2, R1, R2

R1 R2 R3 a b c d t = a - b R1 R2 R3 LD R1, a LD R2, b SUB R2, R1, R2 a t a, R1 b c d R2 No registers are in use - pick the first two available for a and b. Choose to put t in R2 because b is not used again in this block.

R1 R2 R3 a b c d t = a - b R1 R2 R3 LD R1, a LD R2, b SUB R2, R1, R2 a t a, R1 b c d R2 u = a - c LD R3, c SUB R1, R1, R3

R1 R2 R3 a b c d a is already in R1, so no load t = a - b LD R1, a needed. t LD is used R2, later, b so don't overwrite R2. SUB R2, R1, load R2 c into R3. R1 R2 R3 Put result a into R1 b since c a is not d needed t again u v in this block. a t a, R1 b c d R2 u = a - c R1 R2 R3 u t c LD R3, c SUB R1, R1, R3 a b c, R3 d R2 R1

R1 R2 R3 a b c d t = a - b R1 R2 R3 LD R1, a LD R2, b SUB R2, R1, R2 a t a, R1 b c d R2 u = a - c R1 R2 R3 u t c v = t + u LD R3, c SUB R1, R1, R3 a b c, R3 d R2 R1 ADD R3, R2, R1

R1 R2 R3 a b c d t = a - b R1 R2 R3 LD R1, a LD R2, b SUB R2, R1, R2 a t u = a - c R1 R2 R3 a, R1 b c d R2 t and u are already in registers - no loads needed. LD R3, c SUB R1, R1, R3 Perform addition, putting the result into R3; c is no lnger needed in this block. u t c v = t + u R1 R2 R3 u t v a b c, R3 d R2 R1 ADD R3, R2, R1 a b c d R2 R1 R3

R1 R2 R3 u t v a b c d R2 R1 R3 Same state as at end of previous slide

R1 R2 R3 u t v a = d a b c d R2 R1 R3 LD R2, d

R1 R2 R3 u t v a = d R1 R2 R3 u a,d v a b c d R2 R1 R3 LD R2, d R2 b c d,r2 R1 R3 Load d into R2, attach a to R2 as well.

R1 R2 R3 u t v a = d R1 R2 R3 u a,d v d = v + u a b c d R2 R1 R3 LD R2, d R2 b c d,r2 R1 R3 ADD R1, R3, R1

R1 R2 R3 u t v a = d R1 R2 R3 u a,d v d = v + u R1 R2 R3 d a v a b c d R2 R1 R3 LD R2, d R2 b c d,r2 R1 R3 ADD R1, R3, R1 R2 b c R1 R3 u and v are in registers, so no loads needed. Cannot destroy a (exists only in R2) without storing back to memory, so use R1 for result. Move d to R1 from R2.

R1 R2 R3 u t v a = d R1 R2 R3 u a,d v d = v + u R1 R2 R3 d a v exit a b c d R2 R1 R3 LD R2, d R2 b c d,r2 R1 R3 ADD R1, R3, R1 R2 b c R1 R3 ST a, R2 ST d, R1

R1 R2 R3 u t v a b c d R2 R1 R3 a = d LD R2, d We're at the end of the block. Make sure that R1 values R2 of R1 R3and R2 aare stored b back c to memory d t(d and u a v respectively). Value of R3 can be lost - it is a temporary of u a,d v R2 b c d,r2 R1 R3 only this block. d = v + u R1 R2 R3 d a v exit R1 R2 R3 d a v ADD R1, R3, R1 R2 b c R1 R3 ST a, R2 ST d, R1 a,r2 b c d,r1 R3

getreg function x = y op z How do we do this?

getreg function x = y op z "1. If y is currently in a register, pick a register already containing y as Ry. Do not issue a machine instruction to load this register, as none is needed. 2. If y is not in a register, but there is a register currently empty, pick one such register as Ry. [LD Ry, y]

getreg function x = y op z 3. The difficult case occurs when y is not in a register, and there is no register that is currently empty. We need to pick one of the allowable registers anyway, and we need to make it safe to reuse. Let R be a candidate register, and suppose v is one of the variables that the register descriptor for R says is in R. We need to make sure that v's value either is not really needed, or that there is somewhere else we can go to get the value of v. The possibilities are:

getreg function x = y op z (a) If the address descriptor for v says that v is somewhere else besides R, then we are OK.

getreg function x = y op z (b) If v is x, the variable being computed by instruction I, and x is not also one of the other operands of instruction I (z in this example), then we are OK. The reason is that in this case we know this value of x is never again going to be used, so we are free to ignore it.

getreg function x = y op z (c) Otherwise, if v is not used later (that is, after the instruction I, there are no further uses of v, and if v is live on exit from the block, then v is recomputed within the block), then we are OK.

getreg function x = y op z (d) If we are not OK by one of the first three cases, then we need to generate the store instruction ST v, R to place a copy of v in its own memory location. This operation is called a spill." [p. 547-548]

getreg function x = y op z Repeat the above (a) - (d) steps for each variable v currently in R. Let the score of R be the number of ST instructions generated. Choose the R with lowest score to actually use.

getreg function x = y op z We also need a register for the result, Rx. "The issues and options are almost as for y, so we shall only mention the differences. 1. Since a new value of x is being computed, a register that holds only x is always an acceptable choice for Rx. This statement holds even if x is one of y and z, since our machine instructions allow two registers to be the same in one instruction.

getreg function x = y op z 2. If y is not used after instruction I, in the sense described for variable v in item (3c), and Ry holds only y after being loaded, if necessary then Ry can also be used as Rx. A similar option holds regarding z and Rz." [p. 548]

Interesting question Can x := "Hi!"; and y := "Hi!"; be aliases? Yes. Does the language mandate this? No. What can happen in a loop with a string literal? Either. Hmm. Double-check language spec on what has to happen with string literals.