Final Code Generation and Code Optimization.

Slides:



Advertisements
Similar presentations
Register Allocation Consists of two parts: Goal : minimize spills
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.
Register allocation Morgensen, Torben. "Register Allocation." Basics of Compiler Design. pp from (
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.
Idea of Register Allocation x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy}
1 CS 201 Compiler Construction Machine Code Generation.
SSA.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
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.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
Carnegie Mellon Lecture 6 Register Allocation I. Introduction II. Abstraction and the Problem III. Algorithm Reading: Chapter Before next class:
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Register Allocation (via graph coloring)
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Final Code Generation and Code Optimization.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
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)
Ben Livshits Based in part of Stanford class slides from
CS745: Register Allocation© Seth Copen Goldstein & Todd C. Mowry Register Allocation.
1 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
1 Data-Flow Analysis Proving Little Theorems Data-Flow Equations Major Examples.
Data Flow Analysis Compiler Baojian Hua
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Optimization software for apeNEXT Max Lukyanov,  apeNEXT : a VLIW architecture  Optimization basics  Software optimizer for apeNEXT  Current.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Register Allocation John Cavazos University.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 33.
Jeffrey D. Ullman Stanford University. 2 boolean x = true; while (x) {... // no change to x }  Doesn’t terminate.  Proof: only assignment to x is at.
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 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
Register Allocation Harry Xu CS 142 (b) 02/11/2013.
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.
Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.
Register Allocation CS 471 November 12, CS 471 – Fall 2007 Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Lecture 5 Partial Redundancy Elimination
Dataflow Testing G. Rothermel.
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.
Final Code Generation and Code Optimization
Static Single Assignment
Compiler Construction
Code Generation Part II
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

Final Code Generation and Code Optimization

for ( i=0; i < N; i++) { base = &a[0]; crt = *(base + i); } base = &a[0]; for ( i=0; i < N; i++) { crt = *(base + i); } original codeoptimized code

e1 = *(&a[0]+offset +i); e2 = *(&a[0]+offset +j); tmp = &a[0]+offset; e1 = *(tmp +i); e2 = *(tmp +j); original codeoptimized code

y = … x = y; b = x / 2; y = … b = y / 2; original codeoptimized code

if (1) x = y; else x = z; x = y; original codeoptimized code

Basics of Code Optimization and Machine Code Generation Construct Control Flow Graph (CFG) Representation for the Intermediate Code  Algorithm for building CFG Perform Data Flow Analysis to Collect Information Needed for Performing Optimizations  Variable Liveness Analysis Perform Optimizations and Generate Machine Code  Algorithm for Register Allocation

Rationale – A value in a register can be accessed much more efficiently than one in memory Liveness Analysis to build Live Ranges – Identifies durations for which each variable could benefit from using a register Perform Register Allocation – CPU has limited registers  keep frequently used values in registers

21

define x define y use x use y use x define x use y use x define x define y use x use y use x define x use y use x 2 Live Ranges of x 1 Live Range of y Start End Start End

read A D = A+1 read B D = D+B D = A+1 read C D = D+C print A,D A A B B C C D D Register Interference Graph nodes: live ranges edges: live ranges overlap R1R1 R1R1 R2R2 R2R2 R3R3 R3R3 k-coloring, where k is the number of registers

Attempt n-coloring Color the interference graph using R colors where R is the number of registers. Observation: If there is a node n with < R neighbors, then no matter how the neighbors are colored, there will be at least one color left over to color node n. Remove n and its edges to get G’ Repeat the above process to get G’’ ……. If an empty graph results, R-coloring is possible. Assign colors in reverse of the order in which they were removed. 25

Attempt Coloring Contd.. Input: Graph G Output: N-coloring of G While there exists n in G with < N edges do Eliminate n & all its edges from G; list n End while If G is empty the for each node i in list in reverse order do Add i & its edges back to G; choose color for i endfor End if 26

A A B B C C D D R1R1 R1R1 R2R2 R2R2 R3R3 R3R3 A A D D {empty graph} A A D D A A B B C C D D R1R1 R2R2 R1R1 R2R2 R3R3 R3R3

Liveness Analysis and Live Range Construction Global Analysis  Finds what variables are live at basic block boundaries Local Analysis  Finds what variables are live at all points within basic blocks Build Live Ranges

define x define y use x use y use x define x use y use x x x x y x,y Start End x - - x x x x,y - GEN = x KILL = - GEN = y KILL = x GEN = y KILL = - GEN = x KILL = - GEN = - KILL = x,y GEN = x KILL = - GEN = x KILL = - GEN = x KILL = - …… IN OUT OUT(b) = U s in Succ(b) IN(s) IN(b) = (OUT(b) – KILL(b)) U GEN(b)

define x define y use x use y use x define x use y use x define x define y use x use y use x define x use y use x 2 Live Ranges of x 1 Live Range of y Start End Start End

Algorithm for solving data flow equations: For each block B do if B is the exit block then OUT[B] = set of global variables IN[B] = (OUT[B] – KILL[B]) U GEN[B] else OUT[B] = IN[B] = { } endif Endfor DONE = false While not DONE do DONE = true; for each B which is not the exit block do new = U IN[B’] B’ ε SUCC(B) if new != OUT[B] then DONE = false; OUT[B] = new; IN[B] = (OUT[B] – KILL[B]) U GEN[B] Endif Endfor Endwhile