Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.

Slides:



Advertisements
Similar presentations
Static Single-Assignment ? ? Introduction: Over last few years [1991] SSA has been Stablished as… Intermediate program representation.
Advertisements

SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
1 SSA review Each definition has a unique name Each use refers to a single definition The compiler inserts  -functions at points where different control.
8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
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 Machine Code Generation.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Program Representations. Representing programs Goals.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
Cpeg421-08S/final-review1 Course Review Tom St. John.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Code Generation Professor Yihjia Tsai Tamkang University.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
CS 201 Compiler Construction
Register Allocation (via graph coloring)
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.
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Precision Going back to constant prop, in what cases would we lose precision?
CSE P501 – Compiler Construction
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
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.
Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation:  Have each value in a register when it is used. Instruction selection.
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.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
More Code Generation and Optimization Pat Morin COMP 3002.
Simone Campanoni SSA Simone Campanoni
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.
Static Single Assignment
Optimization Code Optimization ©SoftMoore Consulting.
Topic 10: Dataflow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
Unit IV Code Generation
CS 201 Compiler Construction
Code Optimization Overview and Examples Control Flow Graph
Static Single Assignment Form (SSA)
Final Code Generation and Code Optimization
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
Reference These slides, with minor modification and some deletion, come from U. of Delaware – and the web, of course. 4/4/2019 CPEG421-05S/Topic5.
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
8 Code Generation Topics A simple code generator algorithm
Reference These slides, with minor modification and some deletion, come from U. of Delaware – and the web, of course. 4/17/2019 CPEG421-05S/Topic5.
Compiler Construction
Building SSA Harry Xu CS 142 (b) 04/22/2018.
Code Generation Part II
Register Allocation via Coloring of Chordal Graphs
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
(via graph coloring and spilling)
CS 201 Compiler Construction
CS 201 Compiler Construction
Presentation transcript:

Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia

Compiler Structure ● Front End ● Source Code → IR ● Middle End ● IR → IR ● Back End ● IR → Output Form (e.g. machine code, byte code)

Intermediate Representation ● Basic Blocks ● Sequence of linear operations ● Control flow graph ● Connects basic blocks ● Describes one or more functions ● Compiler “middle end” transforms the IR such that the compiler output will ● Perform the same computation ● Run faster on the concrete hardware

SSA Form ● Intermediate Representation is constructed to never re-assign (or mutate) variables. ● (Just add a subscript to each assignment) ● This means that along a given control flow path each variable name represents a value. ● This simplifies a number of optimizations.

Example (1/2) – Rename variables

Example (2/2) – Add “phony” function

Minimal SSA ● We want some basic guarantees ● Minimal number of Φ functions are inserted so that we are sure that: ● Each name is assigned a value exactly once ● Each use of a name in the original program has a unique name to reference ● There is an efficient algorithm to get this

Minimizing SSA – Dominance ● Given an SSA control flow graph, ● We say a node (= basic block) A dominates another node C if ● A and C are the same node OR ● A will always run before C. ● We say that a node B is in the Dominance Frontier of C if ● B does not dominate C AND ● B does dominate an immediate predecessor of C

Dominance Example ● A dominates B ● A dominates C ● B does not dominate C ● B is in the dominance frontier of C ● For block C, how do we tell that we need a Φ func for y but not for x? A BD C

Constant Propagation (1/2) int computify(int x) { int z = 2; x = 14; x = 7 - x / z; return x * (28 / z + 2); } int computify2(int x1) { int z1 = 2; int x2 = 14; int x3 = 7 – x2 / z1; return x3 * (28 / z1 + 2); }

Constant Propagation (2/2) int computify2(int x1) { z1 = 2; x2 = 14; x3 = 7 – x2 / z1; return x3 * (28 / z1 + 2); } int computify3(int x1) { z1 = 2; x2 = 14; x3 = 7 – 14 / 2 = 0; return 0 * (28 / 2 + 2) = 0; }

Dead Code Elimination int computify3(int x1) { z1 = 2; x2 = 14; x3 = 0; return 0; } int computify4(int x1) { return 0; }

Global Value Numbering ● Each expression that produces a value in a graph is numbered. ● Duplicate computations can then be eliminated. a = b * c; d = c; e = b * d; f = e + d + 4; v3 = v1 * v2; v2 = v2; v3 = v3; v4 = v3 + v2 + 4;

Sparse Conditional Constant Propagation ● A more powerful version of constant propagation and dead code elimination. ● Traverse the control flow graph like an interpreter. ● Propagate constant values. ● Variables are unknown ● If a conditional branch can't be resolved, take both ● Any code not reached in the traversal is statically unreachable and can be pruned.

Register Allocation (1/2) ● Modern processors have 8 – 64 registers. ● During a basic block or function, which variables (values) go in which registers when? ● What if there are more variables than registers? ● Spill them to the stack. ● Which variables do we spill?

Register Allocation (2/3) ● Register allocation for one basic block: ● First, calculate the live range of each variable. ● When is it first and last referenced? ● Build a conflict graph. Edges denote variables that are alive at the same time, and thus can't go in the same register. ● If we can color the graph with N colors such that no two neighbors are the same color, that's a register allocation with no spills for N registers.

Register Allocation (3/3) ● The standard algorithm for register allocation, graph coloring, takes worst case exponential time in general. ● Hack and Grund (2005) show that SSA form can reduce the graph coloring problem to polynomial (linear?) time. ● Figuring out the optimal variables to spill to minimize memory access is still NP-complete.