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 (
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}
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)
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.
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
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,
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.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
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)
Final Code Generation and Code Optimization.
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.
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.
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.
Global Register Allocation Based on
Topic Register Allocation
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Dataflow Testing G. Rothermel.
Code Optimization Chapter 10
Code Optimization Chapter 9 (1st ed. Ch.10)
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.
CS 201 Compiler Construction
CS 201 Compiler Construction
Register Allocation Hal Perkins Summer 2004
Static Single Assignment
Compiler Construction
Code Generation Part II
Live variables and copy propagation
(via graph coloring and spilling)
Register Allocation Harry Xu CS 142 (b) 05/08/2018.
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 code optimized code

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

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

if (1) x = y; else x = z; x = y; original code optimized 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

Liveness Analysis to build Live Ranges 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

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

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

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.

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

A A B C {empty graph} D D R1 R2 R3 A A R1 R1 B C {empty graph} R3 R3 D D R2 R2

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

Input a Input b y = a + b y = y – 1 x = a + 1 Print x + y { } { x, y } { y, a } { a, b } { a }

of a function other than main()

Start 1 2 3 4 6 5 7 8 End define x define y use x use y GEN[8] = x KILL[8] = - GEN[6] = y KILL[6] = x GEN[4] = y KILL[4] = - GEN[2] = x KILL[2] = - GEN[7] = x KILL[7] = - GEN[5] = x KILL[5] = - GEN[3] = x KILL[3] = - 1 2 3 4 5 6 7 8 GEN[1] = - KILL[1] = x,y

IN[1] = (OUT[1] – KILL[1]) U GEN[1] = OUT[1] - {x,y} OUT[1] = IN[2] U IN[4] IN[2] = (OUT[2] – KILL[2]) U GEN[2] = OUT[2] U {x} OUT[2] = IN[3] U IN[4] IN[3] = (OUT[3] – KILL[3]) U GEN[3] = OUT[3] U {x} OUT[3] = IN[3] U IN[5] IN[4] = (OUT[4] – KILL[4]) U GEN[4] = OUT[4] U {y} OUT[4] = IN[5] U IN[6] IN[5] = (OUT[5] – KILL[5]) U GEN[5] = OUT[5] U {x} OUT[5] = {} IN[6] = (OUT[6] – KILL[6]) U GEN[6] = (OUT[6] – {x}) U {y} OUT[6] = IN[7] U IN[8] IN[7] = (OUT[7] – KILL[7]) U GEN[7] = OUT[7] U {x} OUT[7] = {} IN[8] = (OUT[8] – KILL[8]) U GEN[8] = OUT[8] U {x} OUT[8] = IN[8] OUT(b) = Us in Succ(b) IN(s) IN(b) = (OUT(b) – KILL(b)) U GEN(b)

IN[1] = OUT[1] - {x,y} OUT[1] = IN[2] U IN[4] IN[2] = OUT[2] U {x} OUT[2] = IN[3] U IN[4] IN[3] = OUT[3] U {x} OUT[3] = IN[3] U IN[5] IN[4] = OUT[4] U {y} OUT[4] = IN[5] U IN[6] IN[5] = OUT[5] U {x} OUT[5] = {} IN[6] = (OUT[6] – {x}) U {y} OUT[6] = IN[7] U IN[8] IN[7] = OUT[7] U {x} OUT[7] = {} IN[8] = OUT[8] U {x} OUT[8] = IN[8] IN[1] {} OUT[1] {x,y} IN[2] OUT[2] IN[3] {x} OUT[3] IN[4] OUT[4] IN[5] OUT[5] IN[6] {y} OUT[6] IN[7] OUT[7] IN[8] OUT[8]

OUT(b) = Us in Succ(b) IN(s) IN(b) = (OUT(b) – KILL(b)) U GEN(b) define x define y use x use y x y x,y Start End - GEN[8] = x KILL[8] = - GEN[6] = y KILL[6] = x GEN[4] = y KILL[4] = - GEN[2] = x KILL[2] = - GEN[1] = - KILL[1] = x,y GEN[7] = x KILL[7] = - GEN[5] = x KILL[5] = - GEN[3] = x KILL[3] = - …… IN OUT 1 2 3 4 5 6 7 8

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

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; Endif Endwhile