Static Single Assignment

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.
Comparison and Evaluation of Back Translation Algorithms for Static Single Assignment Form Masataka Sassa #, Masaki Kohama + and Yo Ito # # Dept. of Mathematical.
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.
6. Intermediate Representation
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
Course Outline Traditional Static Program Analysis Software Testing
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.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
SSA.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
Computer Science 313 – Advanced Programming Topics.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA Emery Berger University.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
CS 201 Compiler Construction
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Intermediate Code. Local Optimizations
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
CSE P501 – Compiler Construction
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Static Single Assignment.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Static Single Assignment Form in the COINS Compiler Infrastructure Masataka Sassa, Toshiharu Nakaya, Masaki Kohama, Takeaki Fukuoka and Masahito Takahashi.
More on Loop Optimization Data Flow Analysis CS 480.
Introduction to SSA Data-flow Analysis Revisited – Static Single Assignment (SSA) Form Liberally Borrowed from U. Delaware and Cooper and Torczon Text.
Computer Science 313 – Advanced Programming Topics.
Loops Simone Campanoni
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Simone Campanoni CFA Simone Campanoni
Data Flow Analysis Suman Jana
Princeton University Spring 2016
Factored Use-Def Chains and Static Single Assignment Forms
University Of Virginia
Control Flow Analysis CS 4501 Baishakhi Ray.
Intermediate Representations
CS 201 Compiler Construction
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Intermediate Representations
Code Optimization Overview and Examples Control Flow Graph
Static Single Assignment Form (SSA)
EECS 583 – Class 8 Classic Optimization
Optimizations using SSA
Data Flow Analysis Compiler Design
Static Single Assignment
9. Optimization Marcus Denker.
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.
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.
SSA-based Optimizations
Building SSA Harry Xu CS 142 (b) 04/22/2018.
CSC D70: Compiler Optimization Static Single Assignment (SSA)
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Code Optimization.
Presentation transcript:

Static Single Assignment Lecture 15 CS 5363 Programming Languages and Compilers

Outline SSA Form Benefit SSA Translation SSA Back Translation

What is SSA Program is in SSA form if every variable is only assigned once Goal: simplify and accelerate optimization process

Example: Sequence Easy to do for sequential programs: SSA Original a := b + c b := c + 1 d := b + c a := a + 1 e := a + b a1 := b1 + c1 b2 := c1 + 1 d1 := b2 + c1 a2 := a1 + 1 e1 := a2 + b2 SSA form makes clear that a2 is not the same as a1, so easier for analysis

Example: Condition Conditions: what to do on control-flow merge? Original SSA if B then a := b else a := c end … a … if B then a1 := b else a2 := c End … a? … is it a1 or is it a2?

Solution: -Function Conditions: what to do on control-flow merge? Original SSA if B then a := b else a := c end … a … if B then a1 := b else a2 := c End a3 := (a1,a2)  selects between a1 and a2

The -Function -functions are always at the beginning of a basic block Select between values depending on control-flow a1 := (a1…ak): the block has k preceding blocks PHI functions evaluated simultaneously within one basic block

SSA and CFG SSA is normally done for control-flow graphs (CFG) Basic blocks are in 3-address form

Review: Control flow graph A CFG models transfer of control in a program nodes are basic blocks (straight-line blocks of code) edges represent control flow (loops, if/else, goto …) if x = y then S1 else S2 end S3

SSA: a Simple Example if B then a1 := 1 else a2 := 2 End a3 := PHI(a1,a2) … a3 …

Repeat: IR front end produces IR optimizer transforms IR to more efficient program back end transform IR to target code

SSA as IR Current trend in compiler community is to use SSA as *the* IR for everything in back end. (NB: for compilers that generate machine code, not those that generate bytecode.)

Why SSA? Making the optimization analysis much simpler We will revisit the common expression and liveness analysis

Common Expression Revisited c = a + b; c1 = a1 + b1; a = b + c; a2 = b1 + c1; d = a + b; d1 = a2 + b1;

Liveness Revisted c = a + b; c1 = a1 + b1; … a = b + c; a2 = b1 + c1; d = a + b; d1 = a2 + b1;

Transform to SSA Two steps: Where to place -functions? Rename Variables Where to place -functions? We want minimal amount of needed  Save memory Algorithms will work faster 16

Path Convergence Criterion There should be a  for a at node Z if: There is a block X containing a definition of a. There is a block Y (Y != X) containing a definition of a. There is a nonempty path Pxz of edges from X to Z.

Path Convergence Criterion There should be a  for a at node Z if: 4. There is a nonempty path Pyz of edges from Y to Z. 5. Path Pxz and Pyz do not have any nodes in common other than Z 6. The node Z does not appear within both Pxz and Pyz prior to the end (although it may appear in one or the other)

Iterated Path-Convergence Inserted  is itself a definition! While there are nodes X,Y,Z satisfying conditions 1-5 and Z does not contain a phi-function for a do insert PHI at node Z.

Example (Simple) 1. block X containing a definition of a 2. block Y (Y != X) containing a definition of a. 3. path Pxz of edges from X to Z. 4. path Pyz of edges from Y to Z. 5. Path Pxz and Pyz do not have any nodes in common other than Z 6. The node Z does not appear within both Pxz and Pyz prior to the end

Dominance Property of SSA Dominance: node D dominates node N if every path from the start node to N goes through D. Dominance is a property of basic blcoks. (one Node dominates a set of nodes). For the dominance property, „definition of x“ thus means the basic block in which x is defined.

Example 5 Dominates all nodes in the gray area

Dominance and SSA Creation Dominance can be used to efficiently build SSA -Functions are placed in all basic blocks of the Dominance Frontier. Dominance Frontier: the set of all nodes N such that D: dominates an immediate predecessor of N but does not dominate N.

Dominance and SSA Creation DF(D) = the set of all nodes N such that D dominates an immediate predecessor of N but does not strictly dominate N. Intuition: Nodes at the border of a region of dominance

Example 5 Dominates all nodes in the gray area

Dominance and SSA Creation Targets of edges from the dominates by 5 to the region not strictly dominated by 5. DF(5)= {4, 5, 12, 13}

Simple Example DF(B1)= DF(B2)= DF(B3)= DF(B4)=

Simple Example DF(B1)={?} DF(B2)= DF(B3)= DF(B4)=

Simple Example DF(B1)={} DF(B2)= DF(B3)= DF(B4)=

Simple Example DF(B1)={} DF(B2)={?} DF(B3)= DF(B4)=

Simple Example DF(B1)={} DF(B2)={B4} DF(B3)= DF(B4)=

Simple Example DF(B1)={} DF(B2)={B4} DF(B3)={B4} DF(B4)=

Simple Example DF(B1)={} DF(B2)={B4} DF(B3)={B4} DF(B4)={}

Simple Example DF(B1)={} DF(B2)={B4} DF(B3)={B4} DF(B4)={} PHI-Function needed in B4 (for a)

Properties of SSA Simplifies many optimizations Examples: Every variable has only one definition Every use knows its definition, every definition knows its uses Unrelated variables get different names Examples: Constant propagation Value numbering Invariant code motion and removal Strength reduction Partial redundancy elimination

SSA in the Real World Invented end of the 80s, a lot of research in the 90s Used in many modern compilers ETH Oberon 2 LLVM GNU GCC 4 IBM Jikes Java VM Java Hotspot VM Mono Many more…

Transforming out-of SSA Processor cannot execute -Function How do we remove it?

Simple Copy Placement

Next Week Advanced Topics Dynamic dispatch Functional Programming and High Order Functions