8 Code Generation Topics A simple code generator algorithm

Slides:



Advertisements
Similar presentations
Target Code Generation
Advertisements

Target code Generation Made by – Siddharth Rakesh 11CS30036 Date – 12/11/2013.
Course Outline Traditional Static Program Analysis Software Testing
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.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
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.
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.
Code Generation Professor Yihjia Tsai Tamkang University.
1 CS 201 Compiler Construction Lecture 1 Introduction.
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.
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.
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.
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.
Execution of an instruction
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
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.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
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
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
©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)
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Code Analysis and Optimization Pat Morin COMP 3002.
Chapter 8 Code Generation
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
High-level optimization Jakub Yaghob
Code Optimization.
Optimization Code Optimization ©SoftMoore Consulting.
Compiler Construction
Code Generation Part I Chapter 9
Code Generation.
Computer Programming Machine and Assembly.
Intermediate Representations
Unit IV Code Generation
Code Generation Part I Chapter 8 (1st ed. Ch.9)
CS 201 Compiler Construction
CS 201 Compiler Construction
Code Generation Part I Chapter 9
Intermediate Representations
Interval Partitioning of a Flow Graph
TARGET CODE -Next Usage
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Compiler Construction
Code Generation Part II
Target Code Generation
TARGET CODE GENERATION
CSc 453 Final Code Generation
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Code Optimization.
CS 201 Compiler Construction
Presentation transcript:

8 Code Generation Topics A simple code generator algorithm Basic problems in storage management, instruction selection, register allocation, and the evaluation order Front-end Code optimization Source code Code generator IR Target program

8.1 Problems in Code Generator Design 8.1.1 Target Program Executable target modules Relocatable machine-language program (object module) Allow modules to be compiled separately Invoke pre-compiled modules Assembly program Generate symbolic instructions and use the macro facilities of the assembler to help generate code Improve readability

8.1 Problems in Code Generator Design 8.1.2 Instruction selection The nature of the instruction set of the target machine has a strong effect on the difficulty of instruction selection, e.g. the uniformity and completeness of the instruction set Instruction speeds and machine idioms are other important factors

8.1 Problems in Code Generator Design If we do not care about the efficiency of the target program, instruction selection is straightforward. Three address code x := y + z (x, y, and z are statically allocated) MOV y, R0 /* load y into register R0 */ ADD z, R0 /* add z to R0 */ MOV R0, x /* store R0 to x */ This strategy often produces redundant loads and stores

8.1 Problems in Code Generator Design sequence of three-address statement a := b + c d := a + e Is translated to MOV b, R0 ADD c, R0 MOV R0, a MOV a, R0 ADD e, R0 MOV R0, d b b+c Redundant instruction. If a isn’t used anymore, instruction 3 is redundant as well

8.1 Problems in Code Generator Design 8.1.3 Register Allocation Registers are the fastest computational unit on the target machine Register allocation select the set of variables that will reside in registers at each point in the program Register assignment pick the specific register that a variable will reside in

8.1 Problems in Code Generator Design 8.1.4 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.

8.2 Target Platform 选择可作为几种微机代表的寄存器机器 四字节组成一个字,有n个通用寄存器R0,R1, …,Rn-1。 8.2.1 Instruction System of the Target Platform 选择可作为几种微机代表的寄存器机器 四字节组成一个字,有n个通用寄存器R0,R1, …,Rn-1。 二地址指令 op 源,目的 MOV {源传到目的} ADD {源加到目的} SUB {目的减去源}

8.2 Target Platform 地址模式和它们的汇编语言形式及附加代价 模式 形式 地址 附加代价 绝对地址 M M 1 模式 形式 地址 附加代价 绝对地址 M M 1 寄存器 R R 0 变址 c(R) c + contents(R) 1 间接寄存器 *R contents(R) 0 间接变址 *c(R) contents(c + contents(R)) 1 直接量 #c (常数)c 1

8.2 Target Platform 指令实例 MOV R0, M MOV 4(R0), M 把 contents(4 + contents(R0)) 放到地址为M的地方去 MOV *4(R0), M 把contents(contents (4 + contents(R0) ) )放到地址为M的地方去 MOV #1, R0

8.2 Target Platform 8.2.2 Program and Instruction Costs Instruction cost = 1 + costs associated with the addressing modes of the operands (source and desitination) Instruction Cost MOV R0,R1 1 MOV R5,M 2 ADD #1, R3 2 SUB 4(R0), *12(R1) 3

8.2 Target Platform a := b + c, a, b, and c are statically allocated Different storage locations lead to different cost a := b + c, a, b, and c are statically allocated MOV b, R0 ADD c, R0 cost= 6 MOV R0, a

8.2 Target Platform a := b + c, a, b, and c are statically allocated MOV b, R0 ADD c, R0 Cost= 6 MOV R0, a MOV b, a ADD c, a Cost= 6

8.2 Target Platform a := b + c, a, b, and c are statically allocated If R0, R1, and R2 contain the address of a, b, and c, respectively, then MOV *R1, *R0 ADD *R2, *R0 Cost= 2

8.2 Target Platform a := b + c, a, b, and c are statically allocated If R0, R1, and R2 contain the address of a, b, and c, respectively, then MOV *R1, *R0 ADD *R2, *R0 Cost= 2 If R1 and R2 contain the values of b and c, and b is no longer used afterwards: ADD R2, R1 MOV R1, a Cost= 3

8.3 Basic Blocks and Flow Graphs maximal sequences of consecutive three-address instructions with the properties that there are no jumps into the middle of the block. Control will leave the block without halting or branching, except possibly at the last instruction in the block. flow graph: basic blocks + control flow information t1 := a * a t2 := a * b t3: = 2 *t2 t4: = t1 + t3 t5: = b * b t6: = t4 + t5

8.3 Basic Blocks and Flow Graphs Algorithm that partitions a sequence of three-address instructions into basic blocks. First, determine the leaders The first three-address instruction in the intermediate code Any instruction that is the target of a conditional or unconditional jump  Any instruction that immediately follows a conditional or unconditional jump Then, for each leader, its basic block consists of itself and all instructions up to but not including the next leader or the end of the intermediate program t1 := a * b …. L1: t2 = … … goto L1; t1 = …

8.3 Basic Blocks and Flow Graphs (1)prod := 0 (2) i := 1 (3) t1 := 4* i (4) t2:= a[t1] (5) t3 := 4* I (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i +1 (11) i := t7 (12) if i <= 20 goto (3) B1 B2 (1) prod := 0 (2) i := 1 (3) t1 := 4* i (4) t2:= a[t1] (5 ) t3 := 4* i (6 ) t4 := b[t3] (7 ) t5 := t2 * t4 (8 ) t6 := prod + t5 (9 ) prod := t6 (10) t7 := i +1 (11) i := t7 (12 ) if i <= 20 goto (3)

8.3 Basic Blocks and Flow Graphs 8.3.2 Transformation of Basic Blocks Three address code x := y + z refers to y, z, and determine x If the value of a name in the basic block is referred at some point after the basic block, we call the name is live at the point Equivalent basic blocks Two basic blocks calculate the same expressions The values of the expressions represent the values of the same live names There are different transformations applicable to basic blocks local global

8.3 Basic Blocks and Flow Graphs Delete local common sub-expression a := b + c a := b + c b := a  d b := a  d c := b + c c := b + c d := a  d d := b Dead code elimination x := y + z not used anymore, x is called dead variable

8.3 Basic Blocks and Flow Graphs Swap neighboring independent statements t1 := b + c t2 := x + y t2 := x + y t1 := b + c If and only if x,y not t1, b,c not t2 Arithmetic transformation x := x + 0 Could be deleted x := x * 1 Could be deleted x := y **2 Equivalent to x := y * y

Revision Instruction cost Determination of basic blocks Transformation of basic blocks