Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.

Slides:



Advertisements
Similar presentations
Target Code Generation
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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 Lecture 3 Data Flow Analysis.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
1 CS 201 Compiler Construction Machine Code Generation.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
1 Compiler Construction Intermediate Code Generation.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Cpeg421-08S/final-review1 Course Review Tom St. John.
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.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 31: Building a Runnable Program COMP 144 Programming Language Concepts Spring 2002.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
CSI Fall 2002 Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany Programming Languages and Systems Concepts Fall.
Improving Code Generation Honors Compilers April 16 th 2002.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
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.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Precision Going back to constant prop, in what cases would we lose precision?
1 CS 201 Compiler Construction Data Flow Analysis.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
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.
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
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.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
CS 404 Introduction to Compiler Design
Intermediate code Jakub Yaghob
Optimization Code Optimization ©SoftMoore Consulting.
Intermediate Representations Hal Perkins Autumn 2011
Code Generation.
Unit IV Code Generation
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Static Single Assignment
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Target Code Generation
CSc 453 Final Code Generation
Intermediate Code Generating machine-independent intermediate form.
Code Optimization.
Presentation transcript:

Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems Concepts Fall 2002 Monday Wednesday 2:30-3:50 LI 99

Introduction to Code Generation  Compilers use multi-phase methods for code generation

Code Generation Vs. Semantic Analysis  Code Generation focus is the last 2 steps.  Code generation uses a more linear (less hierarchichal) representation  Programs are broken into basic blocks  Longest sequential stretches of code  Basic blocks are connected via a control flow graph  Every entry is the destination of an edge, every exit is the source of the edge

Basic Blocks Revisited  So What is a basic block?  Longest sequential code segment guaranteed to run from start to finish.  Begins with either:  A Label (destination of a branch)  The instruction after a branch  Ends with either:  A Label  A branch instruction (conditional or unconditional)

Importance of Basic Blocks  Basic blocks are important because:  They can be optimized  e.g. Instruction reordering can avoid pipeline stalls  Inside a basic block:  In the beginning we can assume an infinite number of registers  But we will have a finite number of registers  So we pick victim registers and spill their contents.

An Example  Find the syntax tree and basic blocks in Euclid's GCD algorithm.

The Syntax Tree  The syntax tree looks like:

Basic Blocks and Call Graph  The Call Graph:  Uses infix  Register a1, a2, rv are used for parameter passage  Temporaries are treated like an infinite pool of registers.

Phases Vs. Passes  Stages of compilation can either:  Pipeline processing between multiple stages  Each stage is called a phase  Later stages get information in incremental chunks  Store the final results of one stage and give the next stage access to the stored results.  Each stage is called a pass.  One stage needs to be memory resident at a time  Can exploit full information

Intermediate Forms  Intermediate Forms (or IFs)  Links the compiler's back end to the front end  Can be classified by level of abstraction  High level IFs use graph or DAG representations  Low level IFs look more like an assembly code  Quads and Triples (2 operand and 3 operand)  High level Ifs support  Incremental compilation  IDE support

Example  P-Code -Pascal, JVM - Java, RTL - GNU  Compilers can have the same front end.  Retargeting the compiler means implementing a new back end.  If you have M front ends and N target architectures  Without IFs you need MŽN frontend/backends  With Ifs you need M front ends + N backends

Address Space  Split data and instruction space  Insert labels  Purge NOPS  Add Memory Management

Live Variable Analysis  Simple idea:  Compute the control flow graph  For each node in the graph  If a variable could be used along some control flow path out of the node, the variable is said to be live  Otherwise (the variable will never be used later) the variable is said to be dead.  So if a dead variable is in a register, that register will be available for use after the last reference.

Some useful Notation  For a basic block B  in[B] are variables that B uses as input.  Or any of B's descendents.  def[B] are variables defined in B.  i.e. On the LHS of an assignment statement  out[B] are variables defined after executing B.  use[B] are variables used by B  i.e. Used as input for an operation

A Live Variable Analysis Algorithm  Input: A Control Flow graph with def and use computed for each block.  Output: Out[B], the set of live-variables at the end of each block's execution.

Global Common Subexpression Elimination  Consider a program with two statements x:=y+z and later w:=y+z  It is expensive to evaluate y+z many times  We can eliminate it as follows:  For each block B using y+z, search back along the arcs of the flow graph stopping at blocks computing y+z, or modify the values of y or z.  Create a new variable u = y+z in blocks that first compute y+z

Global Common Subexpression Elimination  We can eliminate it as follows:  For each block B using y+z, search back along the arcs of the flow graph stopping at blocks computing y+z, or modify the values of y or z.  Create a new variable u := y+z in blocks that first compute y+z  Replace x := y+z with u := y+z; x:= u;  Replace w := y+z with w := u