Introduction to SSA Data-flow Analysis Revisited – Static Single Assignment (SSA) Form Liberally Borrowed from U. Delaware and Cooper and Torczon Text.

Slides:



Advertisements
Similar presentations
Topic G: Static Single-Assignment Form José Nelson Amaral
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.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
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.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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.
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.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 Copy Propagation What does it mean? Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
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.
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.
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)
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic G: Static Single- Assignment Form José Nelson Amaral
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
Static Single Assignment Form (SSA)
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
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?
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
CSE P501 – Compiler Construction
Data Flow Analysis Compiler Baojian Hua
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Static Single Assignment.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
Jeffrey D. Ullman Stanford University. 2 boolean x = true; while (x) {... // no change to x }  Doesn’t terminate.  Proof: only assignment to x is at.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Building SSA Form (A mildly abridged account) For the full story, see the lecture notes for COMP 512 (lecture 8) and.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
Hy-C A Compiler Retargetable for Single-Chip Heterogeneous Multiprocessors Philip Sweany 8/27/2010.
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.
Building SSA Form, I 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Data Flow Analysis Suman Jana
Static Single Assignment
Topic 10: Dataflow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
University Of Virginia
Building SSA Form COMP 512 Rice University Houston, Texas Fall 2003
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Static Single Assignment Form (SSA)
EECS 583 – Class 7 Static Single Assignment Form
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.
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
CSC D70: Compiler Optimization Static Single Assignment (SSA)
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:

Introduction to SSA Data-flow Analysis Revisited – Static Single Assignment (SSA) Form Liberally Borrowed from U. Delaware and Cooper and Torczon Text

Prelude zSSA: A program is said to be in SSA form iff yeach variable is statically defined exactly only once, and yeach use of a variable is dominated by that variable’s definition.

Example zIn general, how to transform an arbitrary program into SSA form? zDoes the definition of X 2 dominate its use in the example? X  x x  X 3   (X 1, X 2 ) 1 2 4

SSA: Motivation zProvide a uniform basis of an IR to solve a wide range of classical dataflow problems zEncode both dataflow and control flow information zA SSA form can be constructed and maintained efficiently zMany SSA dataflow analysis algorithms are more efficient (have lower complexity) than their CFG counterparts.

Static Single- Assignment Form Each variable has only one definition in the program text. This single static definition can be in a loop and may be executed many times. Thus even in a program expressed in SSA, a variable can be dynamically defined many times.

Advantages of SSA Simpler dataflow analysis No need to use use-def/def-use chains, which requires N  M space for N uses and M definitions SSA form relates in a useful way with dominance relation.

SSA Form – An Example SSA-form zEach name is defined exactly once zEach use refers to exactly one name What’s hard zStraight-line code is trivial zSplits in the CFG are trivial zJoins in the CFG are hard Building SSA Form zInsert Ø-functions at birth points zRename all values for uniqueness * x  x  a + b x  y - z x  13 z  x * q s  w - x ? [Curtesy: Slide are from the book wibesite from Prof. K. Cooper’s website]

Birth Points ( another notion due to Tarjan ) Consider the flow of values in this example: x  x  a + b x  y - z x  13 z  x * q s  w - x The value x appears everywhere It takes on several values. Here, x can be 13, y-z, or 17-4 Here, it can also be a+b If each value has its own name … Need a way to merge these distinct values Values are “born” at merge points *

Birth Points ( another notion due to Tarjan ) Consider the flow of values in this example: x  x  a + b x  y - z x  13 z  x * q s  w - x New value for x here or y - z New value for x here 13 or ( or y - z) New value for x here a+b or ((13 or (17-4 or y-z))

Birth Points ( another notion due to Tarjan ) x  x  a + b x  y - z x  13 z  x * q s  w - x These are all birth points for values All birth points are join points Not all join points are birth points Birth points are value-specific … Consider the value flow below:

Review SSA-form zEach name is defined exactly once zEach use refers to exactly one name What’s hard zStraight-line code is trivial zSplits in the CFG are trivial zJoins in the CFG are hard Building SSA Form zInsert Ø-functions at birth points zRename all values for uniqueness A Ø-function is a special kind of copy that selects one of its parameters. The choice of parameter is governed by the CFG edge along which control reached the current block. Real machines do not implement a Ø-function directly in hardware.(not yet!) y 1 ...y 2 ... y 3  Ø(y 1,y 2 ) *

SSA Form in Control- Flow Path Merges b  M[x] a  0 if b<4 a  b c  a + b B1 B2 B3 B4 Is this code in SSA form? No, two definitions of a appear in the code (in B1 and B3) How can we transform this code into a code in SSA form? We can create two versions of a, one for B1 and another for B3.

SSA Form in Control- Flow Path Merges b  M[x] a1  0 if b<4 a2  b c  a? + b B1 B2 B3 B4 But which version should we use in B4 now? We define a fictional function that “knows” which control path was taken to reach the basic block B4: = B3 from B4at arrive weif a2 B2 from B4at arrive weif a1 a2a1,  ( )

SSA Form in Control- Flow Path Merges b  M[x] a1  0 if b<4 a2  b a3   (a2,a1) c  a3 + b B1 B2 B3 B4 But, which version should we use in B4 now? We define a fictional function that “knows” which control path was taken to reach the basic block B4: ( ) = B3 from B4at arrive weif a2 B2 from B4at arrive weif a1 a2, 

A Loop Example a  0 b  a+1 c  c+b a  b*2 if a < N return a1  0 a3   (a1,a2) b2   (b0,b2) c2   (c0,c1) b2  a3+1 c1  c2+b2 a2  b2*2 if a2 < N return  (b0,b2) is not necessary because b0 is never used. But the phase that generates  functions does not know it. Unnecessary functions are eliminated by dead code elimination. Note: only a,c are first used in the loop body before it is redefined. For b, it is redefined right at the Beginning!

The  function How can we implement a  function that “knows” which control path was taken? Answer 1: We don’t!! The  function is used only to connect use to definitions during optimization, but is never implemented. Answer 2: If we must execute the  function, we can implement it by inserting MOVE instructions in all control paths.

Criteria For Inserting  Functions We could insert one  function for each variable at every join point(a point in the CFG with more than one predecessor). But that would be wasteful. What should be our criteria to insert a  function for a variable a at node z of the CFG? Intuitively, we should add a function  if there are two definitions of a that can reach the point z through distinct paths.

Path Convergence Criterion Insert a  function for a variable a at a node z if all the following conditions are true: 1. There is a block x that defines a 2. There is a block y  x that defines a 3. There is a non-empty path Pxz from x to z 4. There is a non-empty path Pyz from y to z 5. Paths Pxz and Pyz don’t have any nodes in common other than z 6. The node z does not appear within both Pxz and Pyz prior to the end, but it might appear in one or the other. The start node contains an implicit definition of every variable.

Iterated Path- Convergence Criterion The  function itself is a definition of a. Therefore the path-convergence criterion is a set of equations that must be satisfied. while there are nodes x, y, z satisfying conditions 1-5 and z does not contain a  function for a do insert a   (a, a, …, a) at node z This algorithm is extremely costly, because it requires the examination of every triple of nodes x, y, z and every path leading from x to y. Can we do better? – a topic for more discussion