Code Generation Simple Register Allocation Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc03.html Chapter 4.2.4.

Slides:



Advertisements
Similar presentations
Target Code Generation
Advertisements

Intermediate Code Generation
Register Allocation CS 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
1 Code generation Our book's target machine (appendix A): opcode source1, source2, destination add r1, r2, r3 addI r1, c, r2 loadI c, r2 load r1, r2 loadAI.
Register Allocation Mooly Sagiv Schrierber Wed 10:00-12:00 html://
1 CS 201 Compiler Construction Machine Code Generation.
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
PART 4: (2/2) Central Processing Unit (CPU) Basics CHAPTER 13: REDUCED INSTRUCTION SET COMPUTERS (RISC) 1.
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
Compiler construction in4020 – lecture 8 Koen Langendoen Delft University of Technology The Netherlands.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Code Generation Mooly Sagiv html:// Chapter 4.
Register Allocation Mooly Sagiv html://
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Liveness Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html://
CS 536 Spring Code generation I Lecture 20.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
Register Allocation (via graph coloring)
Code Generation Mooly Sagiv html:// Chapter 4.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Compiler Summary Mooly Sagiv html://
Activation Records (Introduction) Mooly Sagiv html:// Chapter 6.3.
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Improving Code Generation Honors Compilers April 16 th 2002.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Register Allocation Recap Mooly Sagiv html:// Special Office Hours Wednesday 12-14, Thursday 12-14, Schriber.
Chapter 13 Reduced Instruction Set Computers (RISC) CISC – Complex Instruction Set Computer RISC – Reduced Instruction Set Computer.
Code Generation Mooly Sagiv html:// Chapter 4.
Register Allocation Mooly Sagiv html://
Register Allocation and Spilling via Graph Coloring G. J. Chaitin IBM Research, 1982.
Reduced Instruction Set Computers (RISC)
Reduced Instruction Set Computers (RISC) Computer Organization and Architecture.
4. Processing the intermediate code From: Chapter 4, Modern Compiler Design, by Dick Grunt et al.
Computer Organization and Architecture Reduced Instruction Set Computers (RISC) Chapter 13.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CMPE 511 Computer Architecture A Faster Optimal Register Allocator Betül Demiröz.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Computer architecture Lecture 11: Reduced Instruction Set Computers Piotr Bilski.
Chapter 7 Syntax-Directed Compilation (AST & Target Code) 1.
Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map.
ECEG-3202 Computer Architecture and Organization Chapter 7 Reduced Instruction Set Computers.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
7. Code Generation Chih-Hung Wang Compilers References 1. C. N. Fischer, R. K. Cytron and R. J. LeBlanc. Crafting a Compiler. Pearson Education Inc., 2010.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
Compiler Principles Fall Compiler Principles Lecture 8: Intermediate Representation Roman Manevich Ben-Gurion University.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
More Code Generation and Optimization Pat Morin COMP 3002.
Chapter 8 Code Generation
CS 404 Introduction to Compiler Design
Compilation (Semester A, 2013/14)
Mooly Sagiv html://
Optimization Code Optimization ©SoftMoore Consulting.
Unit IV Code Generation
CS 201 Compiler Construction
Local Optimizations.
Chapter 12 Pipelining and RISC
Compiler Construction
Code Generation Part II
Target Code Generation
(via graph coloring and spilling)
Presentation transcript:

Code Generation Simple Register Allocation Mooly Sagiv html:// Chapter 4.2.4

Basic Compiler Phases

Code generation issues Code selection Register allocation Instruction ordering

Simplifications Consider small parts of AST at time –One expression at the time Target machine simplifications –Ignore certain instructions Use simplifying conventions

Overall Structure

Outline Simple code generation for expressions (4.2.4) –Pure stack machine –Pure register machine Code generation of basic blocks (4.2.5) Automatic generation of code generators (4.2.6) Register Allocation by Graph Coloring (4.2.7)

Simple Code Generation Fixed translation for each node type Translates one expression at the time Local decisions only Works well for simple machine model –Stack machines (PDP 11, VAX) –Register machines (IBM 360/370) Can be applied to modern machines

Simple Stack Machine BP SP Stack

Stack Machine Instructions

Simple Code Generation for Stack Machine Tree rewritings Bottom up AST traversal

Abstract Syntax Trees for Stack Machine Instructions

Simple Code Generation for Stack Machine Tree rewritings Bottom up AST traversal

Example - * * bb4* ac Push_Local #b Mult_Top2 Push_Constant 4 Push_Local #a Push_Local #c Mult_Top2 Subt_Top2

DFS Code Generation

Simple Code Generation for Register Machine Need to allocate register for temporary values –AST nodes The number of machine registers may not suffice Simple Algorithm: –Bottom up code generation –Allocate registers for subtrees

Register Machine Fixed set of registers Load and store from/to memory Arithmetic operations only on registers

Register Machine Instructions

Abstract Syntax Trees for Register Machine Instructions

Simple Code Generation Assume enough registers Use DFS to: –Generate code –Assign Registers Target register Auxiliary registers

Code Generation with Register Allocation

Code Generation with Register Allocation(2)

Example - * * bb4* ac Load_Mem b, R1 Load_Mem b, R2 Mult_Reg R2, R1 Load_Constant 4, R2 Load_Mem a, R3 Load_Mem c, R4 Mult_Reg R4, R3 Mult_Reg R3, R2 Subt_Reg R1, R1 T=R1 T=R2 T=R3 T=R4

Example

Runtime Evaluation

Optimality The generated code is suboptimal May consume more registers than necessary –May require storing temporary results Leads to larger execution time

Example

Observation (Aho&Sethi) The compiler can reorder the computations of sub-expressions The code of the right-subtree can appear before the code of the left-subtree May lead to faster code

Example - * * bb4* ac Load_Mem b, R1 Load_Mem b, R2 Mult_Reg R2, R1 Load_Constant 4, R3 Load_Mem a, R2 Load_Mem c, R3 Mult_Reg R3, R2 Mult_Reg R2, R3 Subt_Reg R3, R1 T=R1 T=R2 T=R3 T=R2 T=R3

Example Load_Mem b, R1 Load_Mem b, R2 Mult_Reg R2, R1 Load_Mem a, R2 Load_Mem c, R3 Mult_Reg R3, R2 Load_Constant 4, R3 Mult_Reg R2, R3 Subt_Reg R3, R1

Two Phase Solution Dynamic Programming Sethi & Ullman Bottom-up (labeling) –Compute for every subtree The minimal number of registers needed Weight Top-Down –Generate the code using labeling by preferring “heavier” subtrees (larger labeling)

The Labeling Principle + m registers n registers m > n m registers

The Labeling Principle + m registers n registers m < n n registers

The Labeling Principle + m registers n registers m = n m+1 registers

The Labeling Procedure

Labeling the example (weight) - * * bb4* ac

Top-Down -3-3 *2 *2*2 b1b1 b1b *2*2 a1a1 c1c1 Load_Mem b, R1 Load_Mem b, R2 Mult_Reg R2, R1 Load_Constant 4, R2 Load_Mem a, R3 Load_Mem c, R2 Mult_Reg R2, R3 Mult_Reg R3, R2 Subt_Reg R2, R1 T=R1 T=R2 T=R3 T=R2

Generalizations More than two arguments for operators –Function calls Register/memory operations Multiple effected registers Spilling –Need more registers than available

Register Memory Operations Add_Mem X, R1 Mult_Mem X, R1 No need for registers to store right operands

Labeling the example (weight) - * * bb4* ac

Top-Down -2-2 *1 *2*2 b1b1 b0b *1*1 a1a1 c0c0 Load_Mem b, R1 Mult_Mem b, R1 Load_Constant 4, R2 Load_Mem a, R1 Mult_Mem c,R1 Mult_Reg R1, R2 Subt_Reg R2, R1 T=R1 T=R2 T=R1

Empirical Results Experience shows that for handwritten programs 5 registers suffice (Yuval 1977) But program generators may produce arbitrary complex expressions

Spilling Even an optimal register allocator can require more registers than available Need to generate code for every correct program The compiler can save temporary results –Spill registers into temporaries –Load when needed Many heuristics exist

Simple Spilling Method A `heavy’ tree contains a `heavy’ subtree whose dependents are ‘light’ Heavy tree – Needs more registers than available Generate code for the light tree Spill the content into memory and replace subtree by temporary Generate code for the resultant tree

Simple Spilling Method

Top-Down (2 registers) -3-3 *2 *2*2 b1b1 b1b *2*2 a1a1 c1c1 Load_Constant 4, R2 Load_Mem a, R2 Load_Mem c, R1 Mult_Reg R1, R2 Mult_Reg R2, R1 T=R1 T=R2 T=R1 Store_Reg R1, T1 T=R1 Load_Mem b, R1 T=R2 Load_Mem b, R2 Mult_Reg R2, R1 Load_Mem T1, R2 Subt_Reg R2, R1

Top-Down (2 registers) Load_Mem a, R2 Load_Mem c, R1 Mult_Reg R1, R2 Load_Constant 4, R2 Mult_Reg R2, R1 Store_Reg R1, T1 Load_Mem b, R1 Load_Mem b, R2 Mult_Reg R2, R1 Load_Mem T1, R2 Subtr_Reg R2, R1

Summary Register allocation of expressions is simple Good in practice Optimal under certain conditions –Uniform instruction cost –`Symbolic’ trees Can handle non-uniform cost –Code-Generator Generators exist (BURS) Even simpler for 3-address machines Simple ways to determine best orders But misses opportunities to share registers between different expressions –Can employ certain conventions Better solutions exist –Graph coloring