Sta$c Single Assignment (SSA) Form

Similar documents
Program analysis for determining opportunities for optimization: 2. analysis: dataow Organization 1. What kind of optimizations are useful? lattice al

Compiler Optimization Intermediate Representation

Compiler Construction 2009/2010 SSA Static Single Assignment Form

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

Introduction to Machine-Independent Optimizations - 6

Code Genera*on for Control Flow Constructs

Dataflow Analysis. Xiaokang Qiu Purdue University. October 17, 2018 ECE 468

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

Sta$c Analysis Dataflow Analysis

Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006)

Static Single Assignment (SSA) Form

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

Vulnerability Analysis (III): Sta8c Analysis

Topic I (d): Static Single Assignment Form (SSA)

Computing Static Single Assignment (SSA) Form. Control Flow Analysis. Overview. Last Time. Constant propagation Dominator relationships

Using Static Single Assignment Form

What If. Static Single Assignment Form. Phi Functions. What does φ(v i,v j ) Mean?

High-Level Synthesis Creating Custom Circuits from High-Level Code

Control-Flow Analysis

A Practical and Fast Iterative Algorithm for φ-function Computation Using DJ Graphs

Lecture 3 Local Optimizations, Intro to SSA

The Static Single Assignment Form:

Sparse Conditional Constant Propagation

Calvin Lin The University of Texas at Austin

Approach starts with GEN and KILL sets

Extending Heuris.c Search

Constant Propagation with Conditional Branches

The Development of Static Single Assignment Form

Intermediate Representation (IR)

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment

Sparse Conditional Constant Propagation

Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA

Sparse Conditional Constant Propagation

Compila(on /15a Lecture 6. Seman(c Analysis Noam Rinetzky

SSA Construction. Daniel Grund & Sebastian Hack. CC Winter Term 09/10. Saarland University

Lecture 1 Introduc-on

Ways to implement a language

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

6. Intermediate Representation

Intermediate representation

A Sparse Algorithm for Predicated Global Value Numbering

Func%onal Programming in Scheme and Lisp

Func%onal Programming in Scheme and Lisp

Why Global Dataflow Analysis?

Control Flow Analysis & Def-Use. Hwansoo Han

Construc)on of Sta)c Single- Assignment Form

A Propagation Engine for GCC

Compiler: Control Flow Optimization

Topics. Data Types, Control Flow, Func9ons & Programming. Data Types. Vectoriza9on 8/22/10

8. Static Single Assignment Form. Marcus Denker

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

CS 61C: Great Ideas in Computer Architecture. MIPS Instruc,on Representa,on II. Dan Garcia

What is Search For? CS 188: Ar)ficial Intelligence. Constraint Sa)sfac)on Problems Sep 14, 2015

6. Intermediate Representation!

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Lecture 23 CIS 341: COMPILERS

CSCI 599 Class Presenta/on. Zach Levine. Markov Chain Monte Carlo (MCMC) HMM Parameter Es/mates

Agenda. Excep,ons Object oriented Python Library demo: xml rpc

CS153: Compilers Lecture 23: Static Single Assignment Form

Proofs about Programs

Op#miza#on Problems, John Gu7ag MIT Department of Electrical Engineering and Computer Science LECTURE 2 1

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

CompSci Understanding Data: Theory and Applica>ons

Ar#ficial Intelligence

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

: Advanced Compiler Design. 8.0 Instruc?on scheduling

Intermediate Representations Part II

Thinking Induc,vely. COS 326 David Walker Princeton University

Example. Example. Constant Propagation in SSA

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Compiler Optimisation

EECS 583 Class 6 More Dataflow Analysis

Reuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont)

GCC Internals Alias analysis

Lecture 2. White- box Tes2ng and Structural Coverage (see Amman and Offut, Chapter 2)

Induction Variable Identification (cont)

Generalizing Map- Reduce

Trustworthy Keyword Search for Regulatory Compliant Records Reten;on

RV: A Run'me Verifica'on Framework for Monitoring, Predic'on and Mining

Amol Deshpande, University of Maryland Lisa Hellerstein, Polytechnic University, Brooklyn

Overview. A mathema5cal proof technique Proves statements about natural numbers 0,1,2,... (or more generally, induc+vely defined objects) Merge Sort

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Contents of Lecture 2

Link State Rou.ng Reading: Sec.ons 4.2 and 4.3.4

Set 3: Informed Heuris2c Search. ICS 271 Fall 2012

Data Flow Analysis and Computation of SSA

Introduc4on to Algorithms

Towards an SSA based compiler back-end: some interesting properties of SSA and its extensions

Advanced Compilers CMPSCI 710 Spring 2003 Dominators, etc.

CSCI 360 Introduc/on to Ar/ficial Intelligence Week 2: Problem Solving and Op/miza/on. Instructor: Wei-Min Shen

LISP: LISt Processing

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Uninformed search strategies

A main goal is to achieve a better performance. Code Optimization. Chapter 9

Review for Final Exam

SSA. Stanford University CS243 Winter 2006 Wei Li 1

Fluxo. Improving the Responsiveness of Internet Services with Automa7c Cache Placement

A System for Genera/ng Sta/c Analyzers for Machine Instruc/ons (TSL)

Transcription:

Sta$c Single Assignment (SSA) Form

SSA form Sta$c single assignment form Intermediate representa$on of program in which every use of a variable is reached by exactly one defini$on Most programs do not sa$sfy this condi$on (eg) see program on next slide: use of Z in node F is reached by defini$ons in nodes A and C Requires inser$ng dummy assignments called Φ- func$ons at merge points in the CFG to merge mul$ple defini$ons Simple algorithm (see transformed example on next slide): Insert Φ- func$ons for all variables at all merge points in the CFG Solve Reaching Defini$ons Rename each real and dummy assignment of a variable uniquely

SSA example A START Z:= A START Z0:= B C Z:=. p1 E p2 D G p3 C B Z2:=. Z1 := Φ(Z4,Z0) p1 E D G Z3:= Φ(Z1,Z3) p3 Z4:= Φ(Z2,Z3) p2 F print(z) F print(z4) END END

Minimal SSA form In previous example, dummy assignment Z3 is not really needed since there is no actual assignment to Z in nodes D and G of the original program Minimal SSA form SSA form of program that does not contain such unnecessary dummy assignments See example on next slide Ques$on: how do we construct minimal SSA form directly? Place φ- func$ons Perform renaming

Minimal- SSA form Example

Intui$on for Φ- func$on Placement Compute Merge rela$on M: V P(V) If node N contains an assignment to a variable x, then node Z is in M(N) if: 1. There is a non- null path P1 := N + Z The value computed at X reaches Z 2. There is a non- null path P2 := START + Z 3. P1 and P2 are disjoint except for Z x = If S V where there are assignments to variable x, then place φ func$ons for x in nodes M(N) N S φ Z φ Z

Dominance fron$er Dominance fron$er of node w Node u is in dominance fron$er of node w if w dominates a CFG predecessor v of u, but does not strictly dominate u Dominance fron$er = control dependence in reverse graph! A B C D E F G Example from previous slide A B C D E F G x x x x x

Iterated dominance fron$er Irreflexive transi$ve closure of dominance fron$er rela$on Related no$on: iterated control dependence in reverse graph Where to place Φ- func$ons for a variable Z Let Assignments = {START} U {nodes with assignments to Z in original CFG} Find set I = iterated dominance fron$er of nodes in Assignments Place Φ- func$ons in nodes of set I For example Assignments = {START,A,C} DF(Assignments) = {E} DF(DF(Assignments)) = {B} DF(DF(DF(Assignments))) = {B} So I = {E,B} This is where we place Φ- func$ons, which is correct

Variable Renaming Use in a non- φ statement: Use immediately domina$ng defini$on of V (+ φ nodes inserted for V ) Use in a φ operand: Use defini$on that immediately dominates incoming CFG edge (not φ)

Compu$ng SSA form Cytron et al algorithm compute DF rela$on (see slides on compu$ng control- dependence rela$on) find irreflexive transi$ve closure of DF rela$on for set of assignments for each variable Compu$ng full DF rela$on Cytron et al algorithm takes O( V + DF ) $me DF can be quadra$c in size of CFG Faster algorithms O( V + E ) $me per variable: see Bilardi and Pingali

Using SSA for Op$miza$on

Constant Propaga$on as an Example (i) x := 1; y := x + 2; if (x>z) then y:= 5; fi y x := 1; y := 3; if (1>z) then y:= 5; fi y Constant propaga$on may simplify control flow as well (ii) x := 1; y := x + 2; if (y>x) then y:= 5; fi y x := 1; y := 3; dead code if (true) then y:= 5; fi 5

Overview of algorithm Build CFG of program makes control flow explicit Perform symbolic evalua$on to determine constants Replace constant- valued variables uses by their values and simplify expressions and control- flow

x := 1; y := x + 2; if (y>x) then y:= 5; fi y Step 1: Build the CFG

Step 2: Symbolic Evalua$on Over CFG Propagate values from following laoce Definitely not constant/can t determine to be a constant Yet to be determined Two operators Join(a,b): lowest value above both a and b (also wriqen as a b) Meet(a,b): highest value below both a and b (also wriqen as a b) Symbolic interpreta$on of expressions EVAL(e, Vin): if any argument of e is T (or ) in Vin, return T (or respec$vely); otherwise, evaluate e normally and return the value

Dataflow Algorithm 1. Associate one state vector with each edge of CFG 2. Set each entry of state vector on edge out of start to T, and place this edge in worklist 3. while (worklist not empty) { Edge ed := worklist.getrandom(); Vin := state- vector[ed] // Symbolically evaluate target node of the edge using state vectors on inputs // and propagate result state vector to output edge of node if (target[ed] is x:= e ) { Propagate Vin[EVAL[e,Vin)/x] to output edge; } else if (target[ed] is switch(p) ) { if (EVAL(p, Vin) is T) Propagate Vin to all outputs of switch; else if (EVAL(p, Vin) is true) Propagate Vin to true side of switch; else Propagate Vin to false side of switch; } else // target node is merge Propagate join of state vectors on all inputs to output } If this changes output state vector, enqueue output edge on worklist }

Applying Algorithm on Running Example

Subtle$es of Algorithm First $me through loop, use of x in loop is determined to be constant 1. Next $me though loop, it reaches final value T.

Algorithm Complexity Height of laoce := 2 each state vector can change value 2*V $mes So while loop in algorithm is executed at most 2*E*V $mes Cost of each itera$on: O(V) Overall algorithm takes O(EV 2 ) $me

Op$mizing Constant Propaga$on Itera$ve procedure is just a method to solve laoce equa$ons Op$mize by exploi$ng sparsity in the dataflow equa$ons Usually, a dataflow equa$on involves only a small number of dataflow variables

Op$mizing Constant Propaga$on Current algorithm uses the CFG to propagate state vectors Propaga$ng informa$on for all variables in lock- step forces a lot of useless copying of informa$on from one vector to another e.g. a variable defined at the top of the procedure and used only at the boqom Solu$on: Do constant propaga$on for each variable separately Propagate informa$on directly from defini?ons to uses, skipping over irrelevant por$ons of control flow graph

Constant Propaga$on Using Def- Use Chains 1. Associate cell with each lhs and rhs occurrence of all variables, ini$alize to 2. Propagate T along each def- use edge out of START, and enqueue target statements of def- use edges onto worklist 3. Enqueue all defini$ons with constant RHS onto worklist 4. while (worklist not empty) { Def d := worklist.getnext(); cell[lhs[d]] := Evaluate(RHS[d]) // using cell[var], var in RHS[d] if (cell[lhs[d]] changes) { Propagate cell[lhs[d]] value along def- use chains to each use stmt //(take join of cell[lhs[d]] and cell value at use) if (cell[use] changes && use is defini$on) worklist.add(use) } }

Example

Analysis of Use- Def Based Constant Propaga$on Complexity: O(sizeof(def- use chains)) This can be as large as O(N 2 V), where N is # CFG- Nodes With SSA this is reduced to O(EV) Problem with algorithm: Loss of accuracy Propaga$on along def- use chains cannot determine directly that y := 45 is dead code, so last use of y is not marked constant We compute def- use chains before doing constant propaga$on, so we don t recognize dead code Possible solu$on: Repeated cycles of reaching defini$ons computa$on, constant propaga$on and dead code elimina$on Is there a beqer way? Key idea: Find unreachable statements during constant propaga$on Do not propagate values out of unreachable defini$ons

High Level View of Poten$al Solu$on Use Control Dependence and Def- Use chains Control Dependence: Node n is control dependent on predicate p if p determines whether n is executed Conven$on: assume START is a predicate, so uncondi$onally executed statements are control dependent on START CDG: Control Dependence Graph

High Level Idea Propagate liveness along control dependence edges while propaga$ng constants along Def- Use chains

Revised Algorithm 1. Associate cell with each lhs and rhs occurrence of all variables and with each statement, ini$alize to 2. Propagate T along each Def- Use edge and control dependence edge out of START. If value in any target cell changes, enqueue target statement onto worklist 3. while (worklist not empty) { Stmt d := worklist.getnext(); if (CDEP- cell[d] is T) { switch (type of d) { case(defini$on): { cell[lhs[d]] := Evaluate(RHS[d]) // using cell[var], var in RHS[d] if (cell[lhs[d]] changes) { Propagate cell[lhs[d]] value along def- use chains to each use stmt //(take join of cell[lhs[d]] and cell value at use) if (cell[use] changes) // if cell value at use changes worklist.add(use) } } case(switch): { Evaluate predicate and propagate along appropriate CDEP edges out of predicate if (cell value at target changes) worklist.add(target) } } } }

Observa$ons We do not propagate informa$on out of dead (unreachable) statements Precision is s$ll not as good as CFG algorithm We s$ll propagate informa$on out of statements that are executed but are irrelevant to output Need algorithm to compute control dependences in general graph Size of CDG: O(EN) (can be reduced)

Problema$c Case START x :=1 y := x + 2 y>x y := 5 merge y

Solu$ons Require that a variable assigned on one side of a condi$onal be assigned on both sides of condi$onal (by inser$ng dummy assignments of form x:= x). Programmers don t want to do this Make compiler insert dummy assignments. Hard to figure out in presence of unstructured control flow Use SSA form: ensure that every use is reached by exactly one defini$on by inser$ng φ- func$ons at merges to combine reaching defini$ons

SSA Algorithm for Constant Propaga$on φ- func$on combines different reaching defini$ons at a merge into a single one at output of merge φ- func$on is like a pseudo- assignment Control dependence at merge: compute for each side of the merge separately Constant propaga$on: Similar to previous algorithm, but at merge, propagate join of inputs only from live sides of merge Minimal SSA permits Def- Use chains to bypass a merge if same defini$on reaches all sides of merge

Sparse Dataflow Evaluator Graphs Same idea can be applied to other dataflow problems Perform dataflow for each sub- problem separately (e.g. for each expression separately in available expressions problem) Build a sparse graph in which only statements that modify or use dataflow informa$on for sub- problem are present and solve that Sparse dataflow evaluator graph can be built in O ( E ) $me per problem (Pingali & Bilardi PLDI 96)

Sparse Dataflow Evaluator Graphs

When is SSA form useful? For many dataflow problems, SSA form enables sparse dataflow analysis that yields the same precision as bit- vector CFG- based dataflow analysis but is asympto$cally faster since it permits the exploita$on of sparsity SSA has two dis$nct features factored def- use chains (more compact than base def- use) renaming you do not have to perform renaming to get advantage of SSA for many dataflow problems The bit- vector approach allows an implicit form of parallelism to be exploited When a problem is not formulated using the bit- vector approach, SSA is preferable Constant propaga$on Useful in pointer analysis Value numbering