Static Single Assignment Form (SSA)

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.
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.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
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.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
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 Global Optimizations Lecture 23.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
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.
Loop invariant detection using SSA An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose single.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
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.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Precision Going back to constant prop, in what cases would we lose precision?
CSE P501 – Compiler Construction
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.
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.
Introduction to SSA Data-flow Analysis Revisited – Static Single Assignment (SSA) Form Liberally Borrowed from U. Delaware and Cooper and Torczon Text.
Loops Simone Campanoni
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Simone Campanoni SSA Simone Campanoni
Introduction to Optimization
Data Flow Analysis Suman Jana
Static Single Assignment
© Seth Copen Goldstein & Todd C. Mowry
Program Representations
Efficiently Computing SSA
Topic 10: Dataflow Analysis
Introduction to Optimization
Factored Use-Def Chains and Static Single Assignment Forms
University Of Virginia
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Optimizations using SSA
Data Flow Analysis Compiler Design
EECS 583 – Class 7 Static Single Assignment Form
Introduction to Optimization
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.
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.
EECS 583 – Class 7 Static Single Assignment Form
SSA-based Optimizations
Building SSA Harry Xu CS 142 (b) 04/22/2018.
Live variables and copy propagation
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Presentation transcript:

Static Single Assignment Form (SSA) Andrew McCreight (based on Prof. Shao’s slides) CS421 11/18/05

Improving Compiler Efficiency Many optimization passes Lots of time spent optimizing Worth it to have new IR for opt. translation to/from new IR (cost) easier/faster opt. (benefit) so let’s extend the basic CFG

Static Single Assignment Main idea: each assignment has a unique name v := 4 z := v + 5 v := 6 y := v + 7 if P then v := 4 else v := 6 u := v + y v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y SSA transformation

Φ-Functions To support this: special Φ-functions are added to each branch join point Φ(v1, v2, …) is equal to vi, if we take the i-th predecessor to get to it if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y if P then v := 4 else v := 6 u := v + y SSA transformation

Dominance Also: a def must dominate all of its uses Statement a dominates statement b if along all valid paths from the entry to the exit, you reach a before you reach b. v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y

What it buys us Easy to determine def of each use: there’s only one! Less need for data flow analysis v := 4 z := v + 5 v := 6 y := v + 7 if P then v := 4 else v := 6 u := v + y v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y SSA transformation use-def chains are much simpler

More efficiency We’ve basically pre-computed some dataflow information, which allows us to avoid redoing it with multiple passes Before, there were a quadratic number of use-def chains (in terms of edges) Now, there are a linear number of them Asymptotic efficiency improvement

What’s the catch? More variables Increase in code size due to Φ-functions But only linearly (in practice, SSA is 0.6-2.4 times larger) Some optimizations are more annoying (things that copy blocks) But on the whole, a win for compilers

Sparse Analysis Some properties are global v1 is always 4, everywhere in the program Don’t have to track the value of v at each point This means it is sparse v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y

Sparse Constant Prop Keep a global table of things that are const At each instruction, check each use to see if it is constant. If you don’t know yet, find out, save the answer. Only have to examine each def once. At phi, only const if all args are same const v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y

Sparse conditional const prop We can be more clever, by evaluating branches. Track which blocks are run. First, assume only first block is run Ignore vals from unrun blocks If ends with a jump, add next block, check it If ends with a branch, see if we can figure out result of test. If we can, only do taken branch next. If we can’t, do both branches Repeat, until no more blocks to examine.

Construction First, decide what variables need Φ-functions, and where Next, rename the variables First step is harder For second, just give each assignment a unique name, do a reaching definitions kind of analysis

Placing Φ Functions Need a Φ function at every join point where two different defs of the same variable reach Want to minimize the number of them Can be computed efficiently (see Appel ch. 19)

De-SSA Eventually, we want to leave SSA Drop all subscripts Simplest way: convert all phi statements to move instructions (in predecessor block) Many of these moves can be eliminated if you are more clever, or through later optimization