Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.

Slides:



Advertisements
Similar presentations
Target Code Generation
Advertisements

Intermediate Code Generation
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
1 CS 201 Compiler Construction Machine Code Generation.
SSA.
CS 31003: Compilers Introduction to Phases of Compiler.
1 Compiler Construction Intermediate Code Generation.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction.
CS 536 Spring Code generation I Lecture 20.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
Code Generation Professor Yihjia Tsai Tamkang University.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Chapter 9: Subprogram Control
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Code Generation.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
Compiler Construction
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Compiler Chapter# 5 Intermediate code generation.
December 8, 2003Other ISA's1 Other ISAs Next, we discuss some alternative instruction set designs. – Different ways of specifying memory addresses – Different.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
1 Code Generation. 2 Position of a Code Generator in the Compiler Model Front-End Code Optimizer Source program Symbol Table Lexical error Syntax error.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Compilers Modern Compiler Design
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Topic #9: Target Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Code Generation Part I Chapter 8 (1st ed. Ch.9)
More Code Generation and Optimization Pat Morin COMP 3002.
Chapter 8 Code Generation
CS 404 Introduction to Compiler Design
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Advanced Computer Systems
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Optimization Code Optimization ©SoftMoore Consulting.
Code Generation Part I Chapter 9
Code Generation.
Intermediate Representations
Unit IV Code Generation
Chapter 6 Intermediate-Code Generation
Code Generation Part I Chapter 8 (1st ed. Ch.9)
Code Generation Part I Chapter 9
Intermediate Representations
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Code Generation Part II
Target Code Generation
Presentation transcript:

Code Generation Ⅰ CS308 Compiler Theory1

2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the semantic meaning of the source program and being of high quality –Making effective use of the available resources of the target machine –The code generator itself must run efficiently. A code generator has three primary tasks: –Instruction selection, register allocation, and instruction ordering

3 Issue in the Design of a Code Generator General tasks in almost all code generators: instruction selection, register allocation and assignment. –The details are also dependent on the specifics of the intermediate representation, the target language, and the run-time system. The most important criterion for a code generator is that it produce correct code. Given the premium on correctness, designing a code generator so it can be easily implemented, tested, and maintained is an important design goal.

4 Input to the Code Generator The input to the code generator is –the intermediate representation of the source program produced by the frontend along with –information in the symbol table that is used to determine the run-time address of the data objects denoted by the names in the IR. Choices for the IR –Three-address representations: quadruples, triples, indirect triples –Virtual machine representations such as bytecodes and stack-machine code –Linear representations such as postfix notation –Graphical representation such as syntax trees and DAG’s Assumptions –Relatively lower level IR –All syntactic and semantic errors are detected.

5 The Target Program The instruction-set architecture of the target machine has a significant impact on the difficulty of constructing a good code generator that produces high-quality machine code. The most common target-machine architecture are RISC, CISC, and stack based. –A RISC machine typically has many registers, three-address instructions, simple addressing modes, and a relatively simple instruction-set architecture. – A CISC machine typically has few registers, two-address instructions, and variety of addressing modes, several register classes, variable-length instructions, and instruction with side effects. –In a stack-based machine, operations are done by pushing operands onto a stack and then performing the operations on the operands at the top of the stack.

6 The Target Program Java Virtual Machine (JVM) –Just-in-time Java compiler Producing the target program as –An absolute machine-language program –Relocatable machine-language program –An assembly-language program In this chapter –Use very simple RISC-like computer as the target machine. –Add some CISC-like addressing modes –Use assembly code as the target language.

7 Instruction Selection The code generator must map the IR program into a code sequence that can be executed by the target machine. The complexity of the mapping is determined by the factors such as –The level of the IR –The nature of the instruction-set architecture –The desired quality of the generated code

8 Instruction Selection If the IR is high level, use code templates to translate each IR statement into a sequence of machine instruction. –Produces poor code, needs further optimization. If the IR reflects some of the low-level details of the underlying machine, then it can use this information to generate more efficient code sequence.

9 Instruction Selection The nature of the instruction set of the target machine has a strong effect on the difficulty of instruction selection. For example, –The uniformity and completeness of the instruction set are important factors. –Instruction speeds and machine idioms are another important factor. If we do not care about the efficiency of the target program, instruction selection is straightforward. x = y + z  LD R0, y ADD R0, R0, z ST x, R0 a = b + c  LD R0, b d = a + e ADD R0, R0, c ST a, R0 LD R0, a ADD R0, R0,e ST d, R0 Redundant

10 Instruction Selection The quality of the generated code is usually determined by its speed and size. A given IR program can be implemented by many different code sequences, with significant cost differences between the different implementations. A naïve translation of the intermediate code may therefore lead to correct but unacceptably inefficient target code. For example use INC for a=a+1 instead of LD R0,a ADD R0, R0, #1 ST a, R0 We need to know instruction costs in order to design good code sequences but, unfortunately, accurate cost information is often difficult to obtain.

11 Register Allocation A key problem in code generation is deciding what values to hold in what registers. Efficient utilization is particularly important. The use of registers is often subdivided into two subproblems: 1.Register Allocation, during which we select the set of variables that will reside in registers at each point in the program. 2.Register assignment, during which we pick the specific register that a variable will reside in. Finding an optimal assignment of registers to variables is difficult, even with single-register machine. Mathematically, the problem is NP-complete.

12 Register Allocation Example 8.1:

13 Evaluation Order The order in which computations are performed can affect the efficiency of the target code. Some computation orders require fewer registers to hold intermediate results than others. However, picking a best order in the general case is a difficult NP- complete problem.

14 The Target Language We shall use as a target language assembly code for a simple computer that is representative of many register machines.

15 A Simple Target Machine Model Our target computer models a three-address machine with load and store operations, computation operations, jump operations, and conditional jumps. The underlying computer is a byte-addressable machine with n general- purpose registers. Assume the following kinds of instructions are available: –Load operations –Store operations –Computation operations –Unconditional jumps –Conditional jumps

16 A Simple Target Machine Model Assume a variety of addressing models: –A variable name x referring o the memory location that is reserved for x. –Indexed address, a(r), where a is a variable and r is a register. –A memory can be an integer indexed by a register, for example, LD R1, 100(R2). –Two indirect addressing modes: *r and *100(r) –Immediate constant addressing mode

17 A Simple Target Machine Model Example 8.2: x = y –z  LD R1, y LD R2, z SUB R1, R1, R2 ST x, R1 b = a[i]  LD R1, i MUL R1, R1, 8 LD R2, a(R1) ST b, R2 a[j] = c  LD R1, c LD R2, j MUL R2, R2, 8 ST a(R2), R1 x = *p  LD R1, p LD R2, 0(R1) ST x, R2 *p = y  LD R1, p LD R2, y ST 0(R1), R2 if x < y goto L  LD R1, x LD R2, y SUB R1, R1, R2 BLTZ R1, L

18 Program and Instruction Costs For simplicity, we take the cost of an instruction to be one plus the costs associated with the addressing modes of the operands. Addressing modes involving registers have zero additional cost, while those involving a memory location or constant in them have an additional cost f one. For example, –LD R0, R1 cost = 1 –LD R0, M cost = 2 –LD R1, *100(R2) cost = 3

19 Addresses in the Target Code We show how names in the IR can be converted into addresses in the target code by looking at code generation for simple procedure calls and returns using static and stack allocation. Program runs in its own logical address space that was partitioned into four code and data areas: 1.A statically determined area Code that holds the executable target code. 2.A statically determined data area Static, for holding global constants and other data generated by the compiler. 3.A dynamically managed area Heap for holding data objects that are allocated and freed during program execution. 4.A dynamically managed area Stack for holding activation records as they are created and destroyed during procedure calls and returns.

20 Static Allocation Focus on the following three-address statements: –Call callee –Return –Halt –Action

21 Static Allocation Store return address and return control to caller Store ST callee.staticArea, #here + 20 BR callee.codeArea Return BR *callee.staticArea

22 Static Allocation Example:

23 Stack Allocation The first procedure initializes the stack by setting SP to the start of the stack area: For sequentially called procedure: Return –Callee: –Caller:

24 Runtime Addresses for Names Assumption: a name in a three-address statement is really a pointer to a symbol-table entry for that name. Note that names must eventually be replaced by code to access storage locations Example: x=0 –Suppose the symbol-table entry for x contains a relative address 12 –x is in a statically allocated area beginning at address static –the actual run-time address of x is static + 12 –The actual assignment: static [ 12] = 0 –For a static area starting at address 100: LD 112, #0

Basic Blocks and Flow Graphs Introduce a graph representation of intermediate code that is helpful for discussing code generation –Partition the intermediate code into basic blocks –The basic blocks become the nodes of a flow graph, whose edges indicate which blocks can follow which other blocks. CS308 Compiler Theory25

Basic Blocks Algorithm: Partitioning three-address instructions into basic blocks. INPUT: A sequence of three-address instructions. OUTPUT: A list of the basic blocks for that sequence in which each instruction is assigned to exactly one basic block. METHOD: First, we determine those instructions in the intermediate code that are leaders. rules for finding leaders are: 1. The first three-address instruction in the intermediate code is a leader. 2. Any instruction that is the target of a conditional or unconditional jump is a leader. 3. Any instruction that immediately follows a conditional or unconditional jump is a leader. CS308 Compiler Theory26

Basic Blocks Find the leaders CS308 Compiler Theory27

Next-Use Information The use of a name in a three-address statement: –Three-address statement i assigns a value to x –Statement j has x as an operand –Control can flow from statement i to j along a path that has no intervening assignments to x –Then statement j uses the value of x computed at i. –Say that x is live at statement i. CS308 Compiler Theory28

Next-Use Information Algorithm (for x=y+z) : Determining the liveness and next-use information for each statement in a basic block. –INPUT: A basic block B of three-address statements. Assume the symbol table initially shows all nontemporary variables in B as being live on exit. –OUTPUT: At each statement i : x = y + z in B, attach to i the liveness and next-use information of x, y, and z. –METHOD: Start at the last statement in B and scan backwards to the beginning of B. At each statement i: x = y + z in B, do the following: 1. Attach to i the information currently found in the symbol table regarding the next use and liveness of x, y, and z. 2. In the symbol table, set x to "not live" and "no next use." 3. In the symbol table, set y and z to "live" and the next uses of y and z to i. CS308 Compiler Theory29

Flow Graphs The nodes of the flow graph are the basic blocks. There is an edge from block B to block C if and only if it is possible for the first instruction in block C to immediately follow the last instruction in block B. There are two ways that such an edge could be justified: –There is a conditional or unconditional jump from the end of B to the beginning of C. –C immediately follows B in the original order of the three-address instructions, and B does not end in an unconditional jump. CS308 Compiler Theory30

Flow Graphs Example: CS308 Compiler Theory31

Optimization of Basic Blocks Local optimization within each basic block Global optimization –which looks at how information flows among the basic blocks of a This chapter focuses on the local optimization CS308 Compiler Theory32

DAG Representation of Basic Blocks Construct a DAG for a basic block 1. There is a node in the DAG for each of the initial values of the variables appearing in the basic block. 2. There is a node N associated with each statement s within the block. The children of N are those nodes corresponding to statements that are the last definitions, prior to s, of the operands used by s. 3. Node N is labeled by the operator applied at s, and also attached to N is the list of variables for which it is the last definition within the block. 4. Certain nodes are designated output nodes. These are the nodes whose variables are live on exit from the block; that is, their values may be used later, in another block of the flow graph. CS308 Compiler Theory33

DAG Representation of Basic Blocks The DAG representation of a basic block lets us perform –eliminating local common sub-expressions –eliminating dead code –reordering statements that do not depend on one another –applying algebraic laws to reorder operands of three-address instructions CS308 Compiler Theory34

Finding Local Common Sub expressions How about if b and d are live on exit? CS308 Compiler Theory35

Dead Code Elimination Delete from a DAG any root (node with no ancestors) that has no live variables attached. Repeated application of this transformation will remove all nodes from the DAG that correspond to dead code. Example: assume a and b are live but c and e are not. –e, and then c can be deleted. CS308 Compiler Theory36

The Use of Algebraic Identities Eliminate computations Reduction in strength Constant folding 2*3.14 = 6.28 evaluated at compile time Other algebraic transformations –x*y=y*x –x>y and x-y>0 –a= b+c; e=c+d+b; e=a+d; CS308 Compiler Theory37

Representation of Array References x = a[i] a[j]=y killed node CS308 Compiler Theory38

Representation of Array References CS308 Compiler Theory39 a is an array. b is a position in the array a. x is killed by b[j]=y.

Pointer Assignments and Procedure Calls Problem of the following assignments x = *P *q = y –we do not know what p or q point to. –x = *p is a use of every variable –*q = y is a possible assignment to every variable. –the operator =* must take all nodes that are currently associated with identifiers as arguments, which is relevant for dead-code elimination. –the *= operator kills all other nodes so far constructed in the DAG. –Global pointer analyses can be used to limit the set of variables Procedure calls behave much like assignments through pointers. –Assume that a procedure uses and changes any data to which it has access. –If variable x is in the scope of a procedure P, a call to P both uses the node with attached variable x and kills that node. CS308 Compiler Theory40

Reassembling Basic Blocks From DAG 's CS308 Compiler Theory41 b is not live on exit b is live on exit

Reassembling Basic Blocks From DAG 's The rules of reassembling –The order of instructions must respect the order of nodes in the DAG –Assignments to an array must follow all previous assignments to, or evaluations from, the same array –Evaluations of array elements must follow any previous assignments to the same array –Any use of a variable must follow all previous procedure calls or indirect assignments through a pointer. –Any procedure call or indirect assignment through a pointer must follow all previous evaluations of any variable. CS308 Compiler Theory42

Homework Exercise Exercise Exercise 8.5.6(b) CS308 Compiler Theory43