Code Generation for Basic Blocks Introduction Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc03.html Chapter 4.2.5.

Slides:



Advertisements
Similar presentations
Target Code Generation
Advertisements

Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
1 Optimization Optimization = transformation that improves the performance of the target code Optimization must not change the output must not cause errors.
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
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 Lecture 3 Data Flow Analysis.
Register Allocation Mooly Sagiv Schrierber Wed 10:00-12:00 html://
1 CS 201 Compiler Construction Machine Code Generation.
Some Properties of SSA Mooly Sagiv. Outline Why is it called Static Single Assignment form What does it buy us? How much does it cost us? Open questions.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
Control Flow Analysis (Chapter 7) Mooly Sagiv (with Contributions by Hanne Riis Nielson)
Program Representations. Representing programs Goals.
Carnegie Mellon Lecture 6 Register Allocation I. Introduction II. Abstraction and the Problem III. Algorithm Reading: Chapter Before next class:
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
Code Generation Mooly Sagiv html:// Chapter 4.
Register Allocation Mooly Sagiv html://
Cpeg421-08S/final-review1 Course Review Tom St. John.
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.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
Code Generation Simple Register Allocation Mooly Sagiv html:// Chapter
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Register Allocation (via graph coloring)
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic B: Loop Restructuring José Nelson Amaral
Code Generation Mooly Sagiv html:// Chapter 4.
Data-Flow Analysis (Chapter 11-12) Mooly Sagiv Make-up class 18/ :00 Kaplun 324.
Overview of program analysis Mooly Sagiv html://
Intermediate Code. Local Optimizations
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.
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.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Code Generation Mooly Sagiv html:// Chapter 4.
1 CS 201 Compiler Construction Data Flow Analysis.
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.
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,
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
More Code Generation and Optimization Pat Morin COMP 3002.
Chapter 8 Code Generation
CS 404 Introduction to Compiler Design
Compilation (Semester A, 2013/14)
Global Register Allocation Based on
Mooly Sagiv html://
Optimizing Compilers Background
The compilation process
Compilation /17a Lecture 10
Unit IV Code Generation
CS 201 Compiler Construction
CS 201 Compiler Construction
Register Allocation Hal Perkins Summer 2004
Register Allocation Hal Perkins Autumn 2005
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Compiler Construction
Code Generation Part II
Target Code Generation
Code Optimization.
CS 201 Compiler Construction
Presentation transcript:

Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter 4.2.5

The Code Generation Problem Given –AST –Machine description Number of registers Instructions + cost Generate code for AST with minimum cost NPC [Aho 77]

Example Machine Description

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

Basic Block Parts of control graph without split A sequence of assignments and expressions which are always executed together Maximal Basic Block Cannot be extended –Start at label or at routine entry –Ends just before jump like node, label, procedure call, routine exit

Example void foo() { if (x > 8) { z = 9; t = z + 1; } z = z * z; t = t – z ; bar(); t = t + 1; x>8 z=9; t = z + 1; z=z*z; t = t - z; bar() t=t+1;

Running Example

Running Example AST

Optimized code(gcc)

Outline Dependency graphs for basic blocks Transformations on dependency graphs From dependency graphs into code –Instruction selection (linearizations of dependency graphs) –Register allocation

Dependency graphs Threaded AST imposes an order of execution The compiler can reorder assignments as long as the program results are not changed Define a partial order on assignments –a < b  a must be executed before b Represented as a directed graph –Nodes are assignments –Edges represent dependency Acyclic for basic blocks

Running Example

Sources of dependency Data flow inside expressions –Operator depends on operands –Assignment depends on assigned expressions Data flow between statements –From assignments to their use Pointers complicate dependencies

Sources of dependency Order of subexpresion evaluation is immaterial –As long as inside dependencies are respected The order of uses of a variable are immaterial as long as: –Come between Depending assignment Next assignment

Creating Dependency Graph from AST 1.Nodes AST becomes nodes of the graph 2.Replaces arcs of AST by dependency arrows Operator  Operand 3.Create arcs from assignments to uses 4.Create arcs between assignments of the same variable 5.Select output variables (roots) 6.Remove ; nodes and their arrows

Running Example

Dependency Graph Simplifications Short-circuit assignments –Connect variables to assigned expressions –Connect expression to uses Eliminate nodes not reachable from roots

Running Example

Cleaned-Up Data Dependency Graph

From Dependency Graph into Code Linearize the dependency graph –Instructions must follow dependency Many solutions exist Select the one with small runtime cost Assume infinite number of registers –Symbolic registers Assign registers later –May need additional spill

Pseudo Register Target Code

Register Allocation Maps symbolic registers into physical registers Reuse registers as much as possible Graph coloring –Undirected graph –Nodes = Registers (Symbolic and real) –Edges = Interference May require spilling

Register Allocation (Example) R1 R2 X1 R3 X1  R2

Running Example

Summary Heuristics for code generation of basic blocks Works well in practice Fits modern machine architecture Can be extended to perform other tasks –Common subexpression elimination But basic blocks are small Can be generalized to a procedure