CSc 453 Final Code Generation

Slides:



Advertisements
Similar presentations
Target Code Generation
Advertisements

Instruction Set Design
Goal: Write Programs in Assembly
Lecture 11: Code Optimization CS 540 George Mason University.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
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 optimization: –A transformation to a program to make it run faster and/or take up less space –Optimization should be safe, preserve the meaning of.
Pipeline Computer Organization II 1 Hazards Situations that prevent starting the next instruction in the next cycle Structural hazards – A required resource.
Computer Organization and Architecture
Chapter 12 CPU Structure and Function. CPU Sequence Fetch instructions Interpret instructions Fetch data Process data Write data.
Computer Organization and Architecture
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
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.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Intermediate Code. Local Optimizations
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.
CH12 CPU Structure and Function
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.
Machine Instruction Characteristics
1 Copyright © 2011, Elsevier Inc. All rights Reserved. Appendix A Authors: John Hennessy & David Patterson.
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.
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.
Computer architecture Lecture 11: Reduced Instruction Set Computers Piotr Bilski.
Chapter 8 CPU and Memory: Design, Implementation, and Enhancement The Architecture of Computer Hardware and Systems Software: An Information Technology.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Basic Memory Management 1. Readings r Silbershatz et al: chapters
High Performance Embedded Computing © 2007 Elsevier Lecture 10: Code Generation Embedded Computing Systems Michael Schulte Based on slides and textbook.
Computer Organization Instructions Language of The Computer (MIPS) 2.
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
Code Optimization.
MIPS Instruction Set Advantages
Chapter 11 Instruction Sets
William Stallings Computer Organization and Architecture 8th Edition
Optimization Code Optimization ©SoftMoore Consulting.
Morgan Kaufmann Publishers
Pipeline Implementation (4.6)
Instruction Scheduling for Instruction-Level Parallelism
Machine-Independent Optimization
Code Generation.
The Multicycle Implementation
CSc 453 Interpreters & Interpretation
These are slides from Comp411
CSCI206 - Computer Organization & Programming
The Multicycle Implementation
Advanced Computer Architecture
Code Optimization Overview and Examples Control Flow Graph
Guest Lecturer TA: Shreyas Chand
Instruction Level Parallelism (ILP)
CS455/CpE 442 Intro. To Computer Architecure
Addressing mode summary
Chapter 12 Pipelining and RISC
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Code Generation Part II
Addressing Modes Don Porter.
Target Code Generation
Chapter 11 Processor Structure and function
Computer Architecture Assembly Language
CSc 453 Interpreters & Interpretation
Code Optimization.
Presentation transcript:

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

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

CSc 453: Final Code Generation 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 Generation

Translating 3-address code to final code Almost a macro expansion process. The resulting code can be improved via various code optimizations. 3-address code MIPS 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 Generation

CSc 453: Final Code Generation 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 Generation

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 Generation

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 Generation

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 Generation

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 Generation

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 Generation