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

Similar documents
Static Single Assignment (SSA) Form

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

Intermediate representation

SSA. Stanford University CS243 Winter 2006 Wei Li 1

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

Live Variable Analysis. Work List Iterative Algorithm Rehashed

Static single assignment

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

Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA

Using Static Single Assignment Form

Introduction to Machine-Independent Optimizations - 6

Induction Variable Identification (cont)

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

Control Flow Analysis & Def-Use. Hwansoo Han

Compiler Construction 2009/2010 SSA Static Single Assignment Form

Introduction to Machine-Independent Optimizations - 4

Calvin Lin The University of Texas at Austin

Lecture 23 CIS 341: COMPILERS

Compiler Optimisation

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

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

Advanced Compilers CMPSCI 710 Spring 2003 Dominators, etc.

Control Flow Analysis

Contents of Lecture 2

Sparse Conditional Constant Propagation

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

Single-Pass Generation of Static Single Assignment Form for Structured Languages

Sparse Conditional Constant Propagation

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

6. Intermediate Representation

8. Static Single Assignment Form. Marcus Denker

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

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

The Development of Static Single Assignment Form

Lecture 8: Induction Variable Optimizations

Sparse Conditional Constant Propagation

6. Intermediate Representation!

Data structures for optimizing programs with explicit parallelism

Rematerialization. Graph Coloring Register Allocation. Some expressions are especially simple to recompute: Last Time

Construc)on of Sta)c Single- Assignment Form

Intermediate Representations Part II

Sta$c Single Assignment (SSA) Form

Data Flow Analysis and Computation of SSA

BYTECODE-LEVEL ANALYSIS AND OPTIMIZATION OF JAVA CLASSES. A Thesis. Submitted to the Faculty. Purdue University. Nathaniel John Nystrom

Algorithms: Lecture 10. Chalmers University of Technology

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

Lecture 3 Local Optimizations, Intro to SSA

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

Control-Flow Analysis

CS 406/534 Compiler Construction Putting It All Together

Extended SSA with factored use-def chains to support optimization and parallelism

Review; questions Basic Analyses (2) Assign (see Schedule for links)

Topic 9: Control Flow

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

CS153: Compilers Lecture 23: Static Single Assignment Form

Code Placement, Code Motion

Example. Example. Constant Propagation in SSA

Combining Optimizations: Sparse Conditional Constant Propagation

BYTECODE-LEVEL ANALYSIS AND OPTIMIZATION OF JAVA CLASSES. A Thesis. Submitted to the Faculty. Purdue University. Nathaniel John Nystrom

EECS 583 Class 8 Classic Optimization

Intermediate Representation (IR)

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

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

CS24 Week 8 Lecture 1

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

Control flow and loop detection. TDT4205 Lecture 29

Code generation for modern processors

Code generation for modern processors

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Simple and Efficient Construction of Static Single Assignment Form

Calvin Lin The University of Texas at Austin

Register Allocation & Liveness Analysis

4/1/15 LLVM AND SSA. Low-Level Virtual Machine (LLVM) LLVM Compiler Infrastructure. LL: A Subset of LLVM. Basic Blocks

Program Representations. Xiangyu Zhang

Lecture 4. More on Data Flow: Constant Propagation, Speed, Loops

Lecture 25 CIS 341: COMPILERS

Register Allocation (wrapup) & Code Scheduling. Constructing and Representing the Interference Graph. Adjacency List CS2210

CSC D70: Compiler Optimization Register Allocation

Lecture 6. Register Allocation. I. Introduction. II. Abstraction and the Problem III. Algorithm

Lecture 9: Loop Invariant Computation and Code Motion

Lecture 6: Analysis of Algorithms (CS )

Control Flow Analysis

EECS 583 Class 6 Dataflow Analysis

The Static Single Assignment Form:

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Compiler Construction 2010/2011 Loop Optimizations

CS 432 Fall Mike Lam, Professor. Data-Flow Analysis

A Loop Optimization Technique Based on Quasi- Invariance

SSI revisited: A Program Representation for Sparse Data-flow Analyses

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Compiler Construction 2016/2017 Loop Optimizations

Range Minimum Queries Part Two

CS4800: Algorithms & Data Jonathan Ullman

Recitation 9. Prelim Review

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

A Sparse Algorithm for Predicated Global Value Numbering

SSA-Form Register Allocation

Lecture Compiler Backend

CS301 - Data Structures Glossary By

Transcription:

Control Flow Analysis Last Time Constant propagation Dominator relationships Today Static Single Assignment (SSA) - a sparse program representation for data flow Dominance Frontier Computing Static Single Assignment (SSA) Form Overview What is SSA? Advantages of SSA over use-def chains Flavors of SSA Dominance frontier Control dependence Inserting φ-nodes Renaming the variables Translating out of SSA form R. Cytron, J. Ferrante, B. K. Rosen, M. N. Wegman, and F. K. Zadeck, Efficiently Computing Static Single Assignment Form and the Control Dependence Graph, ACM TOPLAS 13(4), October, 1991, pp. 451 490. CS 380C Lecture 7 1 Static Single Assignment CS 380C Lecture 7 2 Static Single Assignment

What is SSA? What is SSA? Each assignment to a variable is given a unique name All of the uses reached by that assignment are renamed Easy for straight-line code V 4 V 0 4 V + 5 V 0 + 5 V 6 V 1 6 V + 7 V 1 + 7 if (...) B 2 X 5 B 3 X 3 B 4 Y X What about control flow? = φ-nodes if (...) B 2 X 0 5 B 3 X 1 3 B 4 X 2 φ(x 0, X 1 ) Y X 2 CS 380C Lecture 7 3 Static Single Assignment CS 380C Lecture 7 4 Static Single Assignment

What is SSA? Advantages of SSA over use-def chains I 1 More compact representation Easier to update? B 2 I I + 1 Each USE has only one definition Definitions are explicit merging of values φ-node merge together multiple definitions definitions may reach multiple φ-node I 0 1 B 2 I 1 φ(i 2, I 0 ) I 2 I 1 + 1 CS 380C Lecture 7 5 Static Single Assignment CS 380C Lecture 7 6 Static Single Assignment

Flavors of SSA Motivating Example Where do we place φ-nodes? 1 Condition: If two non-null paths X + Z and Y + Z converge at node Z, and nodes X and Y contain assignments to V (in the original program), then a φ-node for V must be inserted at Z (in the new program). 2 A = 3 4 7 8 A = 9 A = minimal As few as possible subject to condition 5 10 Briggs-minimal Invented by Preston Briggs As few as possible subject to condition, and V must be live across some basic block 6 pruned As few as possible subject to condition, and no dead φ-nodes Where do you put the φ-nodes? CS 380C Lecture 7 7 Static Single Assignment CS 380C Lecture 7 8 Static Single Assignment

Dominance Frontiers Dominance Frontier Example Intuitively: The dominance frontier indicates a join point of control flow where two or more potential definitions can come together. DF(v) dominance frontier of v is a set. DF(v) includes w iff v dominates some predecessor of w v does not strictly dominate w DF(v) = {w ( u PRED(w)) [v DOM u] v DOM! w} Remember: If X appears on every path from entry to Y, then X dominates Y (X DOM Y). If X DOM Y and X Y, then X strictly dominates Y (X DOM! Y). 2 A = 3 4 5 DF(8) = DF(9) = DF(2)= 1 6 7 8 A = 9 A = 10 The immediate dominator of Y (IDOM(Y)) is the closest strict dominator of Y. IDOM(Y) is Y s parent in the dominator tree. DF({8,9}) = DF(10) = DF({2,8,9,10}) = CS 380C Lecture 7 9 Static Single Assignment CS 380C Lecture 7 10 Static Single Assignment

Dominance Frontier Dominance Frontier Example Intuitively: The dominance frontier indicates a join point of control flow where two or more potential definitions can come together. DF(v) dominance frontier of v is a set. DF(v) includes w iff v dominates some predecessor of w v does not strictly dominate w DF(v) = {w ( u PRED(w)) [v DOM u] v DOM! w} Algorithm: procedure FindDF(v) forall v if (the number of predecessors of v 2) then forall predecessors p of v runner = p while (runner IDOM(v) add v to DF(runner) runner = IDOM(runner) endwhile endfor endif 1 2 A = 7 3 4 8 A = 9 A = 5 10 6 DF(1) = DF(6) = DF(2) = DF(7) = DF(3) = DF(8) = DF(4) = DF(9) = DF(5) = DF(10) = CS 380C Lecture 7 11 Static Single Assignment CS 380C Lecture 7 12 Static Single Assignment

Iterated Dominance Frontier Extend the dominance frontier mapping from nodes to sets of nodes: DF(L) = S X L DF(X) The iterated dominance frontier DF + (L) is the limit of the sequence: DF 1 = DF(L) DF i+1 = DF(L S DF i ) Theorem 1 The set of nodes that need φ-nodes for any variable V is the iterated dominance frontier DF + (L), wherel is the set of nodes with assignments to V. Inserting φ-nodes for each variable V HasAlready /0 EverOnWorkList /0 WorkList /0 for each node X containing an assignment to V EverOnWorkList EverOnWorkList S {X} WorkList WorkList S {X} end for while WorkList /0 remove X from WorkList for each Y DF(X) if Y HasAlready insert a φ-node for V at Y HasAlready HasAlready S {Y } if Y EverOnWorkList EverOnWorkList EverOnWorkList S {Y } WorkList WorkList S {Y } end for end while endfor CS 380C Lecture 7 13 Static Single Assignment CS 380C Lecture 7 14 Static Single Assignment

Inserting φ-node Example 1 Renaming the variables Data Structures 2 A = 3 4 7 8 A = 9 A = Stacks array of stacks, one for each original variable V The subscript of the most recent definition of V Initially, Stacks[V] = EmptyStack, V Counters an array of counters, one for each original variable The number of assignments to V processed Initially, Counters[V] = 0, V 5 6 10 procedure GenName(Variable V) i Counters[V] replace V by V i Push i onto Stacks[V] Counters[V] i + 1 Rename - a recursive procedure Walks the control flow graph in preorder Initially, call Rename(entry) CS 380C Lecture 7 15 Static Single Assignment CS 380C Lecture 7 16 Static Single Assignment

Renaming the variables procedure Rename(Block X) if X visited return for each φ-node P in X GenName(LHS(P)) for each statement A in X for each variable V RHS(A) replace V by V i, where i = Top(Stacks[V]) for each variable V LHS(A) GenName(V) for each Y SUCC(X) j position in Y s φ-nodes corresponding to X for each φ-node P in Y replace the j th operand of RHS(P) by V i where i = Top(Stacks[V]) for each Y SUCC(X) Rename(Y) for each φ-node or statement A in X for each V i LHS(A) Pop (Stacks[V]) What happens to Stacks during Renaming? V. V. V Stacks Before V i 0 Stacks After V i+3 i+2 i+1 i 0 CS 380C Lecture 7 17 Static Single Assignment CS 380C Lecture 7 18 Static Single Assignment

Computing SSA Form Translating Out of SSA Form Compute dominance frontiers Insert φ-nodes Rename variables Theorem 2 Any program can be put into minimal SSA form using this algorithm. Translating Out of SSA Form Restore original names to variables Delete all φ-nodes Replace φ-nodes with copies in predecessors if (...) B 2 X 0 5 B 3 X 1 3 B 4 X 2 φ(x 0, X 1 ) Y X 2 if (...) X 0 5 X 1 3 B 2 B 3 X 2 X 0 X 2 X 1 B 4 Y X 2 CS 380C Lecture 7 19 Static Single Assignment CS 380C Lecture 7 20 Static Single Assignment

Next Time Cytron et al. Dominance Frontier Algorithm Static Single Assignment let SUCC(S) = [ s S SUCC(s) Induction variables (standard vs. SSA) Loop Invariant Code Motion with SSA DOM! 1 (v) = DOM 1 (v) - v, then DF(v) = SUCC(DOM 1 (v)) - DOM! 1 (v) [ DF(v) = DF local (v) DF up (c) c Child(v) Child(v): children of v in the dominator tree DF local (v) = {w w SUCC(v) vdom!w} DF up (w) is the subset of DF(w) that is not strictly dominated by IDOM(w) (IDOM(w) = v). Algorithm: procedure FindDF(v) DF(v) = empty for w DomChild(v) do FindDF(w) for u in DF(w) do if not( v DOM! u) then add u to DF(v) endfor endfor for w in SUCC(v) do if not( v DOM! w) then add w to DF(v) endfor CS 380C Lecture 7 21 Static Single Assignment CS 380C Lecture 7 22 Static Single Assignment