SSA.

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

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.
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.
Register allocation Morgensen, Torben. "Register Allocation." Basics of Compiler Design. pp from (
Static Single Assignment (SSA) Form Jaeho Shin :00 ROPAS Weekly Show & Tell.
Lecture 11: Code Optimization CS 540 George Mason University.
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.
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.
Some Properties of SSA Mooly Sagiv. Outline Why is it called Static Single Assignment form What does it buy us? How much does it cost us? Open questions.
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.
1 Constant Propagation for Loops with Factored Use-Def Chains Reporter : Lai, Yen-Chang.
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.
Early Global Program Optimizations Chapter Mooly Sagiv.
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.
Data Flow Analysis Compiler Design Nov. 3, 2005.
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
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.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
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
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Static Single Assignment.
Dominators, control-dependence and SSA form. Organization Dominator relation of CFGs –postdominator relation Dominator tree Computing dominator relation.
Using SSA Dead Code Elimination & Constant Propagation C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
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.
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.
Generating SSA Form (mostly from Morgan). Why is SSA form useful? For many dataflow problems, SSA form enables sparse dataflow analysis that –yields the.
Final Code Generation and Code Optimization.
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.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Building SSA Form, I 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Data Flow Analysis Suman Jana
Static Single Assignment
Efficiently Computing SSA
Factored Use-Def Chains and Static Single Assignment Forms
University Of Virginia
Building SSA Form COMP 512 Rice University Houston, Texas Fall 2003
CS 201 Compiler Construction
Optimizations using SSA
Data Flow Analysis Compiler Design
Final Code Generation and Code Optimization
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

SSA

Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example

SSA Introduction SSA is a intermediate representation. SSA was developed by Ron Cytron, Jeanne Ferrante, Barry Rosen, Mark Wegman, and Ken Zadeck, researchers at IBM in the 1980s. Benefits constant propagation sparse conditional constant propagation dead code elimination global value numbering partial redundancy elimination strength reduction register allocation

SSA Introduction Static Single Assignment Form Each variable has only one reaching definition. When two definitions merge, a Ф function is introduced to with a new definition of the variable. A Ф operand represents the reaching definition from the corresponding predecessor.

SSA Introduction a3= a = a = a1= = a+5 = a1+5 = a+5 a4= Ф(a1,a3) a =

SSA Introduction SSA Condition If two nonnull paths X →+ Z and Y →+ Z converge at a node, and nodes X and Y contain assignments to V (in the original program), then a trivial -function V = (V,…,V) has been inserted at Z (in the new program) Each mention of V in the original program or in an inserted -function has been replaced by a mention of a new variable Vi, leaving the new program in SSA form Along any control flow path, consider any use of a variable V (in the original program) and the corresponding use of Vi (in the new program). Then V and Vi has same value.

SSA Introduction Where to insert -functions? By condition 1, a node Z needs a -function for V because Z is a convergence point for two nonnull paths X →+ Z and Y →+ Z that start at nodes X and Y already containing assignments to V.

SSA Introduction What about arrays? = A[i] = R(A,i) = R(A8,i7) Treating A[i] as a variable would be awkward, both because an assignment to A[i] may or may not change the value of A[j] and because the value of A[i] could be changed by assigning to i rather than to A[i]. Treat entire array as a scalar variable. = A[i] A[j] = V = A[k] = R(A,i) A = W(A,j,V) = R(A,k) = R(A8,i7) A9 = W(A8,j6,V5) = R(A9,k4)

SSA Introduction W operator may introduce unnecessary liveness for A. Introduce HW (HiddenW). repeat A[i] = i i = i +1 until i>10 repeat i2 = Ф(i1,i3) A1 = Ф(A0,A2) A2= W(A1,i2,i2) i3 = i2 +1 until i3>10 repeat i2 = Ф(i1,i3) A2= HW(i2,i2) i3 = i2 +1 until i3>10

Converting to SSA Big picture, translation to SSA form is done in 3 steps The dominance frontier mapping is constructed form the control flow graph. Using the dominance frontiers, the locations of the -functions for each variable in the original program are determined. The variables are renamed by replacing each mention of an original variable V by an appropriate mention of a new variable Vi

CFG A control flow graph G = (V, E) Set V contains distinguished nodes ENTRY and EXIT every node is reachable from ENTRY EXIT is reachable from every node in G. ENTRY has no predecessors EXIT has no successors. Notation: predecessor, successor, path

Dominance Relation If X appears on every path from ENTRY to Y, then X dominates Y. Dominance relation is both reflexive and transitive. idom(Y): immediate dominator of Y Dominator Tree ENTRY is the root Any node Y other than ENTRY has idom(Y) as its parent Notation: parent, child, ancestor, descendant

Dominator Tree Example ENTRY ENTRY a a EXIT b c b c d d CFG DT EXIT

Dominator Tree Example

Dominance Frontier Dominance Frontier DF(X) for node X Set of nodes Y X dominates a predecessor of Y X does not strictly dominate Y Equation 1: DF(X) = {Y|(P∈Pred(Y))(XP and XY )} Equation 2: DF(X) = DFlocal(X)∪ ∪Z∈Children(X)DFup(z) DFlocal(X) = {Y∈Succ(x)|idom(Y)≠X} DFup(X) = {Y∈DF(Z)|idom(Z) ≠X}

Dominance Frontier How to proof equation 1 and equation 2 are correct? Easy to See that => is correct Still have to show everything in DF(X) has been accounted for. Suppose Y ∈DF(X) and UY be the edge that X dominate U but doesn’t strictly dominate Y. If U == X, then Y ∈ DFlocal(X) If U ≠X, then there exists a path from X to U in Dominator Tree which implies there exists a child Z of X dominate U. Z doesn’t strictly dominate Y because X doesn’t strictly dominate Y. So Y ∈DFup(X).

Ф-function and Dominance Frontier Intuition behind dominance frontier Y ∈DF(X) means: Y has multiple predecessors X dominate one of them, say U, U inherits everything defined in X Reaching definition of Y are from U and other predecessors So Y is exactly the place where Ф-function is needed

Control Dependences and Dominance Frontier A CFG node Y is control dependent on a CFG node X if both the following hold: There is nonnull path p: X →+ Y such that Y postdominate every node after X. The node Y doesn’t strictly postdominate the node X If X appears on every path from Y to Exit, then X postdominate Y.

Control Dependences and Dominance Frontier In other words, there is some edge from X that definitely causes Y to execute, and there is also some path from X that avoids executing Y.

RCFG The reverse control flow graph RCFG has the same nodes as CFG, but has edge Y →X for each edge X→Y in CFG. Entry and Exit are also reversed. The postdominator relation in CFG is dominator relation in RCFG. Let X and Y be nodes in CFG. Then Y is control dependent on X in CFG iff X∈DF(Y) in RCFG.

SSA Construction– Place Ф Functions For each variable V Add all nodes with assignments to V to worklist W While X in W do For each Y in DF(X) do If no Ф added in Y then Place (V = Ф (V,…,V)) at Y If Y has not been added before, add Y to W.

SSA Construction– Rename Variables Rename from the ENTRY node recursively For node X For each assignment (V = …) in X Rename any use of V with the TOS of rename stack Push the new name Vi on rename stack i = i + 1 Rename all the Ф operands through successor edges Recursively rename for all child nodes in the dominator tree Pop Vi in X from the rename stack

Rename Example TOS a= a1= a1+5 Rename expr a= Ф(a1,a) a = a+5 a+5 a1

Converting out of SSA Eventually, a program must be executed. The Ф-function have precise semantics, but they are generally not represented in existing target machines.

Converting Out of SSA Naively, a k-input Ф-function at entrance to node X can be replaced by k ordinary assignments, one at the end of each control predecessor of X. Inefficient object code can be generated.

Dead Code Elimination Where does dead code come from? Assignments without any use Dead code elimination method Initially all statements are marked dead Some statements need to be marked live because of certain conditions Mark these statements can cause others to be marked live. After worklist is empty, truly dead code can be removed

Allocation By Coloring It might seem possible to map all occurrence of Vi back to V and delete all Ф-functions. After certain optimization, this assumption might not be true.

Allocation By Coloring Use graph coloring techniques to allocate storage for variables. In the case above,V1 and V2 are alive simultaneously, they have different color, and will be converted back to two different variables.

SSA Example Dead Code Elimination Intuition Because there is only one definition for each variable, if the list of uses of the variable is empty, the definition is dead. When a statement v x  y is eliminated because v is dead, this statement must be removed from the list of uses of x and y. This might cause those definitions to become dead.

SSA Example Simple Constant Propagation Intuition If there is a statement v  c, where c is a constant, then all uses of v can be replaced for c. A  function of the form v  (c1, c2, …, cn) where all ci are identical can be replaced for v  c. Using a work list algorithm in a program in SSA form,we can perform constant propagation in linear time.

SSA Example B1 i  1 j  1 k 0 B2 if k<100 B3 B4 if j<20 while(k<100) { if(j<20) j=i; k=k+1; } else j=k; k=k+2; return j; B2 if k<100 B3 B4 if j<20 return j B5 B6 j  i k  k+1 j  k k  k+2 B7

SSA Example B1 i  1 j  1 k1 0 B2 if k<100 B3 B4 if j<20 while(k<100) { if(j<20) j=i; k=k+1; } else j=k; k=k+2; return j; B2 if k<100 B3 B4 if j<20 return j B5 B6 j  i k3  k+1 j  k k5  k+2 B7

SSA Example B1 i  1 j  1 k1 0 B2 if k<100 B3 B4 if j<20 while(k<100) { if(j<20) j=i; k=k+1; } else j=k; k=k+2; return j; B2 if k<100 B3 B4 if j<20 return j B5 B6 j  i k3  k+1 j  k k5  k+2 B7 k4  (k3,k5)

SSA Example B1 i  1 j  1 k1 0 B2 k2  (k4,k1) if k<100 B3 B4 while(k<100) { if(j<20) j=i; k=k+1; } else j=k; k=k+2; return j; B2 k2  (k4,k1) if k<100 B3 B4 if j<20 return j B5 B6 j  i k3  k+1 j  k k5  k+2 B7 k4  (k3,k5)

SSA Example B1 i  1 j  1 k1 0 B2 k2  (k4,k1) if k2<100 B3 B4 while(k<100) { if(j<20) j=i; k=k+1; } else j=k; k=k+2; return j; B2 k2  (k4,k1) if k2<100 B3 B4 if j<20 return j B5 B6 j  i k3  k2+1 j  k k5  k2+2 B7 k4  (k3,k5)

SSA Example i1  1 j1  1 k1 0 j3  i1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2  (j4,j1) k2  (k4,k1) if k2<100 j4  (j3,j5) k4  (k3,k5) B1 B2 B3 B5 B6 B4 B7 i=1; j=1; k=0; while(k<100) { if(j<20) j=i; k=k+1; } else j=k; k=k+2; return j;

SSA Example: Constant Propagation j1  1 k1 0 j3  i1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2  (j4,j1) k2  (k4,k1) if k2<100 j4  (j3,j5) k4  (k3,k5) B1 B2 B3 B5 B6 B4 B7 i1  1 j1  1 k1 0 j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2  (j4,1) k2  (k4,0) if k2<100 j4  (j3,j5) k4  (k3,k5) B1 B2 B3 B5 B6 B4 B7

SSA Example: Dead Code Elimination j1  1 k1 0 j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2  (j4,1) k2  (k4,0) if k2<100 j4  (j3,j5) k4  (k3,k5) B1 B2 B3 B5 B6 B4 B7 j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2  (j4,1) k2  (k4,0) if k2<100 j4  (j3,j5) k4  (k3,k5) B2 B3 B5 B6 B4 B7

SSA Example: Constant Propagation and Dead Code Elimination j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2  (j4,1) k2  (k4,0) if k2<100 j4  (j3,j5) k4  (k3,k5) B2 B3 B5 B6 B4 B7 j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2  (j4,1) k2  (k4,0) if k2<100 j4  (1,j5) k4  (k3,k5) B2 B3 B5 B6 B4 B7

SSA Example: One Step Further But block 6 is never executed! How can we find this out, and simplify the program? B2 j2  (j4,1) k2  (k4,0) if k2<100 B3 B4 return j2 SSA conditional constant propagation finds the least fixed point for the program and allows further elimination of dead code. if j2<20 B5 B6 k3  k2+1 j5  k2 k5  k2+2 B7 j4  (1,j5) k4  (k3,k5)

SSA Example: Dead Code Elimination B2 B4 k3  k2+1 return j2 j2  (j4,1) k2  (k4,0) if k2<100 j4  (1) k4  (k3) B2 B5 B7 j2  (j4,1) k2  (k4,0) if k2<100 B3 B4 if j2<20 return j2 B6 k3  k2+1 j5  k2 k5  k2+2 B7 j4  (1,j5) k4  (k3,k5)

SSA Example: Single Argument  Function Elimination k3  k2+1 return j2 j2  (j4,1) k2  (k4,0) if k2<100 j4  (1) k4  (k3) B2 B5 B7 k3  k2+1 return j2 j2  (j4,1) k2  (k4,0) if k2<100 j4  1 k4  k3 B2 B5 B7 B4 B4

SSA Example: Constant and Copy Propagation k3  k2+1 return j2 j2  (j4,1) k2  (k4,0) if k2<100 j4  1 k4  k3 B2 B5 B7 k3  k2+1 return j2 j2  (1,1) k2  (k3,0) if k2<100 j4  1 k4  k3 B2 B5 B7 B4 B4

SSA Example: More Dead Code k3  k2+1 return j2 j2  (1,1) k2  (k3,0) if k2<100 B2 B5 B4 k3  k2+1 return j2 j2  (1,1) k2  (k3,0) if k2<100 j4  1 k4  k3 B2 B5 B7 B4

SSA Example: More  Function Simplification k3  k2+1 return j2 j2  (1,1) k2  (k3,0) if k2<100 B2 B5 B4 k3  k2+1 return j2 j2  1 k2  (k3,0) if k2<100 B2 B5 B4

SSA Example: More Constant Propagation k3  k2+1 return j2 j2  1 k2  (k3,0) if k2<100 B2 B5 B4 k3  k2+1 return 1 j2  1 k2  (k3,0) if k2<100 B2 B5 B4

SSA Example: Ultimate Dead Code Elimination k3  k2+1 return 1 j2  1 k2  (k3,0) if k2<100 B2 B5 B4 return 1 B4