6/9/2015© 2002-08 Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
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.
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.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
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.
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.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Static Single Assignment.
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)
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
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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.
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.
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.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
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.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
12/5/2002© 2002 Hal Perkins & UW CSER-1 CSE 582 – Compilers Data-flow Analysis Hal Perkins Autumn 2002.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Program Representations. Representing programs Goals.
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.
Introduction to SSA Data-flow Analysis Revisited – Static Single Assignment (SSA) Form Liberally Borrowed from U. Delaware and Cooper and Torczon Text.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
Optimization Simone Campanoni
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Introduction to Optimization
Static Single Assignment
Efficiently Computing SSA
Topic 10: Dataflow Analysis
Introduction to Optimization
Instruction Scheduling Hal Perkins Summer 2004
Factored Use-Def Chains and Static Single Assignment Forms
Instruction Scheduling Hal Perkins Winter 2008
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Static Single Assignment Form (SSA)
Register Allocation Hal Perkins Summer 2004
Register Allocation Hal Perkins Autumn 2005
Optimizations using SSA
Data Flow Analysis Compiler Design
Introduction to Optimization
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
Instruction Scheduling Hal Perkins Autumn 2005
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.
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Instruction Scheduling Hal Perkins Autumn 2011
Presentation transcript:

6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008

Agenda Overview of SSA IR Constructing SSA graphs SSA-based optimizations Converting back from SSA form Source: Appel ch. 19, also an extended discussion in Cooper- Torczon sec /9/2015© Hal Perkins & UW CSEU-2

Def-Use (DU) Chains Common dataflow analysis problem: Find all sites where a variable is used, or find the definition site of a variable used in an expression Traditional solution: def-use chains – additional data structure on the dataflow graph Link each statement defining a variable to all statements that use it Link each use of a variable to its definition 6/9/2015© Hal Perkins & UW CSEU-3

DU-Chain Drawbacks Expensive: if a typical variable has N uses and M definitions, the total cost is O(N * M) Would be nice if cost were proportional to the size of the program Unrelated uses of the same variable are mixed together Complicates analysis 6/9/2015© Hal Perkins & UW CSEU-4

SSA: Static Single Assignment IR where each variable has only one definition in the program text This is a single static definition, but it may be in a loop that is executed dynamically many times 6/9/2015© Hal Perkins & UW CSEU-5

SSA in Basic Blocks Original a := x + y b := a – 1 a := y + b b := x * 4 a := a + b SSA a 1 := x + y b 1 := a 1 – 1 a 2 := y + b 1 b 2 := x * 4 a 3 := a 2 + b 2 6/9/2015© Hal Perkins & UW CSEU-6 We’ve seen this before when looking at value numbering

Merge Points The issue is how to handle merge points Solution: introduce a Φ-function a 3 := Φ(a 1, a 2 ) Meaning: a 3 is assigned either a 1 or a 2 depending on which control path is used to reach the Φ-function 6/9/2015© Hal Perkins & UW CSEU-7

Example 6/9/2015© Hal Perkins & UW CSEU-8 b := M[x] a := 0 if b < 4 a := b c := a + b Original b 1 := M[x0] a 1 := 0 if b 1 < 4 a 2 := b 1 a 3 := Φ(a 1, a 2 ) c 1 := a 3 + b 1 SSA

How Does Φ “Know” What to Pick? It doesn’t When we translate the program to executable form, we can add code to copy either value to a common location on each incoming edge For analysis, all we may need to know is the connection of uses to definitions – no need to “execute” anything 6/9/2015© Hal Perkins & UW CSEU-9

Example With Loop 6/9/2015© Hal Perkins & UW CSEU-10 a := 0 b := a + 1 c := c + b a := b * 2 if a < N return c Original a 1 := 0 a 3 := Φ(a 1, a 2 ) b 1 := Φ(b 0, b 2 ) c 2 := Φ(c 0, c 1 ) b 2 := a c 1 := c 2 + b 2 a 2 := b 2 * 2 if a 2 < N return c 1 SSA Notes: a 0, b 0, c 0 are initial values of a, b, c on block entry b 1 is dead – can delete later c is live on entry – either input parameter or uninitialized

Converting To SSA Form Basic idea First, add Φ-functions Then, rename all definitions and uses of variables by adding subscripts 6/9/2015© Hal Perkins & UW CSEU-11

Inserting Φ-Functions Could simply add Φ-functions for every variable at every join point(!) But Wastes way too much space and time Not needed 6/9/2015© Hal Perkins & UW CSEU-12

When to Insert a Φ-Function Insert a Φ-function for variable a at point z when There are blocks x and y, both containing definitions of a, and x  y There are nonempty paths from x to z and from y to z These paths have no common nodes other than z z is not in both paths prior to the end (it may appear in one of them) 6/9/2015© Hal Perkins & UW CSEU-13

Details The start node of the flow graph is considered to define every variable (even if to “undefined”) Each Φ-function itself defines a variable, so we need to keep adding Φ-functions until things converge 6/9/2015© Hal Perkins & UW CSEU-14

Dominators and SSA One property of SSA is that definitions dominate uses; more specifically: If x := Φ(…,x i,…) in block n, then the definition of x dominates the ith predecessor of n If x is used in a non-Φ statement in block n, then the definition of x dominates block n 6/9/2015© Hal Perkins & UW CSEU-15

Dominance Frontier (1) To get a practical algorithm for placing Φ-functions, we need to avoid looking at all combinations of nodes leading from x to y Instead, use the dominator tree in the flow graph 6/9/2015© Hal Perkins & UW CSEU-16

Dominance Frontier (2) Definitions x strictly dominates y if x dominates y and x  y The dominance frontier of a node x is the set of all nodes w such that x dominates s predecessor of w, but x does not strictly dominate w Essentially, the dominance frontier is the border between dominated and undominated nodes 6/9/2015© Hal Perkins & UW CSEU-17

Example 6/9/2015© Hal Perkins & UW CSEU

Placing Φ-Functions If a node x contains the definition of variable a, then every node in the dominance frontier of x needs a Φ- function for a Since the Φ-function itself is a definition, this needs to be iterated until it reaches a fixed- point Theorem: this algorithm places exactly the same set of Φ-functions as the path criterion given previously 6/9/2015© Hal Perkins & UW CSEU-19

Placing Φ-Functions: Details We won’t give the full constructions here. The basic steps are: 1. Compute the dominance frontiers for each node in the flowgraph 2. Insert just enough Φ-functions to satisfy the criterion. Use a worklist algorithm to avoid reexamining nodes unnecessarily 3. Walk the dominator tree and rename the different definitions of variable a to be a 1, a 2, a 3, … 6/9/2015© Hal Perkins & UW CSEU-20

SSA Optimizations A sampler of optimizations that exploit SSA form First, what do we know? (i.e., what information is kept in the SSA graph?) 6/9/2015© Hal Perkins & UW CSEU-21

SSA Data Structures Statement: links to containing block, next and previous statements, variables defined, variables used. Statement kinds are: ordinary, Φ-function, fetch, store, branch Variable: link to definition (statement) and use sites Block: List of contained statements, ordered list of predecessors, successor(s) 6/9/2015© Hal Perkins & UW CSEU-22

Dead-Code Elimination A variable is live iff its list of uses is not empty(!) Algorithm to delete dead code: while there is some variable v with no uses if the statement that defines v has no other side effects, then delete it Need to remove this statement from the list of uses for its operand variables – which may cause those variables to become dead 6/9/2015© Hal Perkins & UW CSEU-23

Simple Constant Propagation If c is a constant in v := c, any use of v can be replaced by c Then update every use of v to use constant c If the ci’s in v := Φ(c1, c2, …, cn) are all the same constant c, we can replace this with v := c Can also incorporate copy propagation, constant folding, and others in the same worklist algorithm 6/9/2015© Hal Perkins & UW CSEU-24

Simple Constant Propagation W := list of all statements in SSA program while W is not empty remove some statement S from W if S is v:=Φ(c, c, …, c), replace S with v:=c if S is v:=c delete S from the program for each statement T that uses v substitute c for v in T add T to W 6/9/2015© Hal Perkins & UW CSEU-25

Converting Back from SSA Unfortunately, real machines do not include a Φ instruction So after analysis, optimization, and transformation, need to convert back to a “Φ-less” form for execution 6/9/2015© Hal Perkins & UW CSEU-26

Translating Φ-functions The meaning of x := Φ(x 1, x 2, …, x n ) is “set x := x 1 if arriving on edge 1, set x:= x 2 if arriving on edge 2, etc.” So, for each i, insert x := x i at the end of predecessor block i Rely on copy propagation and coalescing in register allocation to eliminate redundant moves 6/9/2015© Hal Perkins & UW CSEU-27

SSA There are (obviously) many details needed to fully implement SSA, but these are the main ideas SSA is used in most modern optimizing compilers & has been retrofitted into many older ones (gcc is a well-known example) 6/9/2015© Hal Perkins & UW CSEU-28