CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.

Slides:



Advertisements
Similar presentations
Target Code Generation
Advertisements

Instruction Set Design
Course Outline Traditional Static Program Analysis Software Testing
Lecture 11: Code Optimization CS 540 George Mason University.
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.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
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.
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
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.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
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.
Computer Organization and Architecture The CPU Structure.
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.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Code Generation Professor Yihjia Tsai Tamkang University.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Register Allocation (via graph coloring)
CSC 8505 Compiler Construction Intermediate Representations.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Intermediate Code. Local Optimizations
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
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.
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Part II: Addressing Modes
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Fall 2002 Lecture 14: Instruction Scheduling. Saman Amarasinghe ©MIT Fall 1998 Outline Modern architectures Branch delay slots Introduction to.
Optimization software for apeNEXT Max Lukyanov,  apeNEXT : a VLIW architecture  Optimization basics  Software optimizer for apeNEXT  Current.
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.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
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.
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Execution of an instruction
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
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
High Performance Embedded Computing © 2007 Elsevier Lecture 10: Code Generation Embedded Computing Systems Michael Schulte Based on slides and textbook.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Code Optimization Overview and Examples
High-level optimization Jakub Yaghob
Code Optimization.
William Stallings Computer Organization and Architecture 8th Edition
Machine-Independent Optimization
Unit IV Code Generation
CS 201 Compiler Construction
Code Optimization Overview and Examples Control Flow Graph
Computer Organization and Design Assembly & Compilation
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Taken largely from University of Delaware Compiler Notes
Code Generation Part II
Target Code Generation
TARGET CODE GENERATION
CSc 453 Final Code Generation
CSc 453 Interpreters & Interpretation
Code Optimization.
CS 201 Compiler Construction
Presentation transcript:

CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson

CSc 453: Final Code Generation2 Overview Input: intermediate code program, symbol table Output: target program (asm or machine code).

CSc 453: Final Code Generation3 Issues Memory management: map symbol table entries to machine locations (registers, memory addresses); map labels to instruction addresses. Instruction selection: Peculiarities of the target machine have to be taken into account, e.g.: different kinds of registers, e.g., address, data registers on M68k; implicit register operands in some instructions, e.g., MUL, DIV; branches: addressing modes (PC-relative vs. absolute); span (short vs. long). Performance considerations: Machine-level decisions (register allocation, instruction scheduling) can affect performance.

CSc 453: Final Code Generation4 Translating 3-address code to final code Almost a macro expansion process. The resulting code can be improved via various code optimizations. 3-address codeMIPS assembly code x = A[ i ] load i into reg1 la reg2, A add reg2, reg2, reg1 lw reg2, ( reg2 ) sw reg2, x x = y + z load y into reg1 load z into reg2 add reg3, reg1, reg2 sw reg3, x if x  y goto L load x into reg1 load y into reg2 bge reg1, reg2, L

CSc 453: Final Code Generation5 Storage Allocation Delay decisions about storage allocation until final code generation phase: initialize location field of each identifier/temp to UNDEF; during code generation, first check each operand of each instruction: if location == UNDEF, allocate appropriate-sized space for it (width obtained from type info); update location field in its symbol table entry; update information about next unallocated location. Advantages: machine dependencies (width for each type) pushed to back end; variables that are optimized away, or which live entirely in registers, don’t take up space in memory.

CSc 453: Final Code Generation6 Improving Code Quality 1 Peephole Optimization: traverse the code looking for sequences that can be improved. E.g.: redundant instruction elimination: goto L /* L is next instruction */  L: … control flow optimizations: goto L1  goto L2 … … L1: goto L2 algebraic simplifications, e.g.: x = x+0 } eliminate x = x  1 y = 2  x  y = x+x

CSc 453: Final Code Generation7 Improving Code Quality 2 Register Allocation: place frequently accessed values in registers. Local register allocation : simple algorithms that consider only small segments of code (“basic blocks”). Global register allocation : algorithms that consider the entire body of a function. These are more complex, but are able to keep variables in registers over larger code fragments, e.g., over an entire loop. Good global register allocation can reduce runtime by ~20–40% (Chow & Hennessy 1990).

CSc 453: Final Code Generation8 Improving Code Quality 3 Code Optimization: Examine the program to identify specific program properties (“dataflow analysis”). Use this information to change the code so as to improve its performance. E.g.: invariant code motion out of loops common subexpression elimination dead code elimination

CSc 453: Final Code Generation9 Improving Code Quality 4 Instruction Scheduling: Some instructions take many cycles to execute. On modern architectures, this can cause the instruction pipeline to be blocked (“stalled”) for several cycles. Instruction scheduling refers to choosing an execution order on the instructions that allows useful work to be done by the CPU while it is waiting for an expensive operation to complete.

CSc 453: Final Code Generation10 Improving Code Quality 5 Memory Hierarchy Optimizations: Modern processors typically use a multi-level memory hierarchy (cache, main memory). Accessing main memory can be very expensive. Careful code layout can improve instruction cache utilization: uses execution frequency information; reduces cache conflicts between frequently executed code.

CSc 453: Final Code Generation11 Basic Blocks and Flow Graphs Example: L1: if x > y goto L0 t1 = x+1 x = t1 L0: y = 0 goto L1 For program analysis and optimization, we need to know the program’s control flow behavior. For this, we: group three-address instructions into basic blocks; represent control flow behavior using control flow graphs.

CSc 453: Final Code Generation12 Basic Blocks Definition: A basic block B is a sequence of consecutive instructions such that: 1. control enters B only at its beginning; 2. control leaves B at its end (under normal execution); and 3. control cannot halt or branch out of B except at its end. This implies that if any instruction in a basic block B is executed, then all instructions in B are executed.  for program analysis purposes, we can treat a basic block as a single entity.

CSc 453: Final Code Generation13 Identifying Basic Blocks 1. Determine the set of leaders, i.e., the first instruction of each basic block: the entry point of the function is a leader; any instruction that is the target of a branch is a leader; any instruction following a (conditional or unconditional) branch is a leader. 2. For each leader, its basic block consists of: the leader itself; all subsequent instructions upto, but not including, the next leader.

CSc 453: Final Code Generation14 Control Flow Graphs Definition: A control flow graph for a function is a directed graph G = (V, E) such that: each v  V is a basic block; and there is an edge a  b  E iff control can go directly from a to b. Construction: 1. identify the basic blocks of the function; 2. there is an edge from block a to block b if: i. there is a (conditional or unconditional) branch from the last instruction of a to the first instruction of b ; or ii. b immediately follows a in the textual order of the program, and a does not end in an unconditional branch.

CSc 453: Final Code Generation15 Example int dotprod(int a[], int b[], int N) { int i, prod = 0; for (i = 1; i  N; i++) { prod += a[i]  b[i]; } return prod; } No.Instructionleader?Block No. 1 enter dotprod Y 1 2 prod = i = t1 = 4*i Y 2 5 t2 = a[t1] 2 6 t3 = 4*i 2 7 t4 = b[t3] 2 8 t5 = t2*t4 2 9 t6 = prod+t prod = t t7 = i+i 2 12 i = t if i  N goto retval prod Y 3 15 leave dotprod 3 16 return 3

CSc 453: Final Code Generation16 Program Analysis Example: Liveness Definition: A variable is live at a program point if it may be used at a later point before being redefined. Example: Example application of liveness information: if x is in a register r at a program point, and x is not live, then r can be freed up for some other variable without storing x back into memory.

CSc 453: Final Code Generation17 Liveness Analysis: Overview For each basic block, identify those variables that are: (possibly) used before being redefined; (definitely) redefined before being used. Propagate this information along control flow graph edges. propagate iteratively until no change; amounts to iterative solution of a set of equations; solution gives, at each point, the set of variables that could be used before being redefined.