8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

Static Single-Assignment ? ? Introduction: Over last few years [1991] SSA has been Stablished as… Intermediate program representation.
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.
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.
6. Intermediate Representation
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
1 Authors: Vugranam C. Sreedhar, Roy Dz-Ching Ju, David M. Gilles and Vatsa Santhanam Reader: Pushpinder Kaur Chouhan Translating Out of Static Single.
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.
1 CS 201 Compiler Construction Machine Code Generation.
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.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Stanford University CS243 Winter 2006 Wei Li 1 SSA.
Program Representations. Representing programs Goals.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
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.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
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.
CS 201 Compiler Construction
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
Intermediate Code. Local Optimizations
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
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...
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.
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
Precision Going back to constant prop, in what cases would we lose precision?
CSE P501 – Compiler Construction
Optimization software for apeNEXT Max Lukyanov,  apeNEXT : a VLIW architecture  Optimization basics  Software optimizer for apeNEXT  Current.
7. Just In Time Compilation Prof. O. Nierstrasz Jan Kurs.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Dominators, etc. Emery Berger University.
Static Single Assignment Form in the COINS Compiler Infrastructure Masataka Sassa, Toshiharu Nakaya, Masaki Kohama, Takeaki Fukuoka and Masahito Takahashi.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Detecting Equality of Variables in Programs Bowen Alpern, Mark N. Wegman, F. Kenneth Zadeck Presented by: Abdulrahman Mahmoud.
More on Loop Optimization Data Flow Analysis CS 480.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
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.
Computer Science 313 – Advanced Programming Topics.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Static Single Assignment
Factored Use-Def Chains and Static Single Assignment Forms
Intermediate Representations
Intermediate Representations
Code Optimization Overview and Examples Control Flow Graph
Static Single Assignment Form (SSA)
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.
Building SSA Harry Xu CS 142 (b) 04/22/2018.
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

8. Static Single Assignment Form Marcus Denker

© Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples  Transforming out of SSA 2

© Marcus Denker SSA Static Single Assignment Form  Goal: simplify procedure-global optimizations  Definition: 3 Program is in SSA form if every variable is only assigned once

© Marcus Denker SSA Why Static?  Why Static? —We only look at the static program —One assignment per variable in the program  At runtime variables are assigned multiple times! 4

© Marcus Denker SSA Example: Sequence  Easy to do for sequential programs: 5 a := b + c b := c + 1 d := b + c a := a + 1 e := a + b a 1 := b 1 + c 1 b 2 := c d 1 := b 2 + c 1 a 2 := a e 1 := a 2 + b 2 Original SSA

© Marcus Denker SSA Example: Condition  Conditions: what to do on control-flow merge? 6 if B then a := b else a := c end … a … if B then a 1 := b else a 2 := c End … a? … Original SSA

© Marcus Denker SSA Solution:  -Function  Conditions: what to do on control-flow merge? 7 if B then a := b else a := c end … a … if B then a 1 := b else a 2 := c End a 3 :=  (a 1,a 2 ) … a 3 … Original SSA

© Marcus Denker SSA The  -Function   -functions are always at the beginning of a basic block  Select between values depending on control-flow  a 1 :=  (a 1 …a k ): the block has k preceding blocks PHI-functions are all evaluated simultaneously. 8

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

Repeat: 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 …) © Marcus Denker SSA 10 if x = y then S1 else S2 end S3

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

Repeat: IR © Oscar Nierstrasz Intermediate Representation 12 front end produces IR optimizer transforms IR to more efficient program back end transform IR to target code

SSA as IR © Marcus Denker SSA 13

© Marcus Denker SSA Transforming to SSA  Problem: Performance / Memory —Minimize number of inserted  -functions —Do not spend to much time  Many relatively complex algorithms —We do not go too much into details —See literature! 14

© Marcus Denker SSA Minimal SSA  Two steps: —Place  -functions —Rename Variables  Where to place  -functions?  We want minimal amount of needed  —Save memory —Algorithms will work faster 15

© Marcus Denker SSA Path Convergence Criterion  There should be a  for a at node Z if: 1.There is a block X containing a definition of a. 2.There is a block Y (Y != X) containing a definition of a. 3.There is a nonempty path P xz of edges from X to Z. 4.There is a nonempty path P yz of edges from Y to Z. 5.Path P xz and P yz do not have any nodes in common other than Z 6.The node Z does not appear within both P xz and P yz prior to the end (although it may appear in one or the other) 16

© Marcus Denker SSA Iterated Path-Convergence  Inserted  is itself a definition! 17 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. A bit slow, other algorithms used in practice

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

© Marcus Denker SSA Dominance Property of SSA  Dominance: node D dominates node N if every path from the start node to N goes through D. (“strictly dominates”: D!=N) 19 Dominance Property of SSA: 1.If x is used in a Phi-function in block N, then the definition of x dominates every predecessor of N. 2.If x is used in a non-Phi statement in N, then the definition of x dominates N “Definition dominates use”

© Marcus Denker SSA 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 strictly dominate N. 20

Dominance Frontier 21 Marcus Denker SSA 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 Dominance and SSA Creation

Dominance Frontier 22 © Marcus Denker DF(D) = the set of all nodes N such that D dominates an immediate predecessor of N but does not strictly dominate N. ? Dominance and SSA Creation SSA

Dominance Frontier 23 © Marcus Denker 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 Dominance and SSA Creation SSA

24 © Marcus Denker Dominance and SSA Creation SSA

25 © Marcus Denker 5 Dominates all nodes in the gray area Dominance and SSA Creation SSA

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

Simple Example 27 © Marcus Denker DF(B1)= DF(B2)= DF(B3)= DF(B4)= SSA

Simple Example 28 © Marcus Denker DF(B1)={?} DF(B2)= DF(B3)= DF(B4)= SSA

Simple Example 29 © Marcus Denker DF(B1)={} DF(B2)= DF(B3)= DF(B4)= SSA

Simple Example 30 © Marcus Denker DF(B1)={} DF(B2)={?} DF(B3)= DF(B4)= SSA

Simple Example 31 © Marcus Denker DF(B1)={} DF(B2)={B4} DF(B3)= DF(B4)= SSA

Simple Example 32 © Marcus Denker DF(B1)={} DF(B2)={B4} DF(B3)={B4} DF(B4)= SSA

Simple Example 33 © Marcus Denker DF(B1)={} DF(B2)={B4} DF(B3)={B4} DF(B4)={} SSA

Simple Example 34 © Marcus Denker DF(B1)={} DF(B2)={B4} DF(B3)={B4} DF(B4)={} PHI-Function needed in B4 (for a) SSA

© Marcus Denker SSA Properties of SSA  Simplifies many optimizations —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 35 Next Week!

© Marcus Denker SSA 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… 36

© Marcus Denker SSA Transforming out-of SSA  Processor cannot execute  -Function  How do we remove it? 37

© Marcus Denker SSA Simple Copy Placement 38

© Marcus Denker SSA Problems  Problems: —Copies need to be removed —Wrong in some cases after reordering of code 39 OriginalSSA with opt  removed

© Marcus Denker SSA  -Congruence  Insert Copies  Rename Variables 40 Idea: transform program so that all variables in  are the same: a1 =  (a1, a1) a1 = a1 --->

© Marcus Denker SSA  -Congruence: Definitions 41  -connected(x): a3 =  (a1, a2) a5 =  (a3, a4) --> a1, a4 are connected  -congruence-class: Transitive closure of  -connected(x).

© Marcus Denker SSA  -Congruence Property 42  -congruence property: All variables of the same congruence class can be replaced by one representative variable without changing the semantics. SSA without optimizations has  -congruence property Variables of the congruence class never live at the same time (by construction)

Repeat: Liveness © Oscar Nierstrasz Code Generation 43 A variable v is live on edge e if there is a path from e to a use of v not passing through a definition of v a and b are never live at the same time, so two registers suffice to hold a, b and c

Interference © Marcus Denker SSA 44 A variable v is live on edge e if there is a path from e to a use of v not passing through a definition of v a, c live at the same time: interference

© Marcus Denker SSA  -Removal: Big picture 45 CSSA: SSA with  -congruence-property. - directly after SSA generation - no interference TSSA: SSA without  -congruence-property. - after optimizations - interference 1.Transform TSSA into CSSA (fix interference) 2.Rename  -variables 3.Delete 

© Marcus Denker SSA Example: Problematic case 46 X2 and X3 interfereSolution: Break up

© Marcus Denker SSA SSA and Register Allocation  Idea: remove  as late as possible  Variables in  -function never live at the same time! —Can be stored in the same register  Do register allocation on SSA! 47

© Marcus Denker SSA SSA: Literature 48 Books: - SSA Chapter in Appel Modern Compiler Impl. In Java - Chapter 8.11 Muchnik: Advanced Compiler Construction SSA Creation: Cytron et. al: Efficiently computing Static Single Assignment Form and the Control Dependency Graph (TOPLAS, Oct 1991) PHI-Removal: Sreedhar et at. Translating out of Static Single Assigment Form (LNCS 1694)

© Marcus Denker SSA Summary 49  SSA, what it is and how to create it —Where to place  -functions?  Transformation out of SSA —Placing copies —Remove  Next Week: Optimizations

© Marcus Denker SSA 50 What you should know!  When a program has SSA form.  What is a  -function.  When do we place  -functions  How to remove  -functions

© Marcus Denker SSA 51 Can you answer these questions?  Why can we not directly generate executable code from SSA?  Why do we use 3-adress code and CFG for SSA?

© Oscar Nierstrasz SSA 52 License > Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.