Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA

Similar documents
Compiler Construction 2009/2010 SSA Static Single Assignment Form

Static single assignment

Intermediate representation

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

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

SSA. Stanford University CS243 Winter 2006 Wei Li 1

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

Static Single Assignment (SSA) Form

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

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

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

8. Static Single Assignment Form. Marcus Denker

Advanced Compilers CMPSCI 710 Spring 2003 Dominators, etc.

Control-Flow Analysis

Control Flow Analysis & Def-Use. Hwansoo Han

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

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

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

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

6. Intermediate Representation

Example. Example. Constant Propagation in SSA

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

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

A Sparse Algorithm for Predicated Global Value Numbering

Introduction to Machine-Independent Optimizations - 6

Control Flow Analysis

6. Intermediate Representation!

CS153: Compilers Lecture 23: Static Single Assignment Form

Compiler Optimisation

The Development of Static Single Assignment Form

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

Sta$c Single Assignment (SSA) Form

Data Flow Analysis and Computation of SSA

Topic 9: Control Flow

Contents of Lecture 2

Introduction to Machine-Independent Optimizations - 4

Calvin Lin The University of Texas at Austin

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

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Lecture 8: Induction Variable Optimizations

Lecture 23 CIS 341: COMPILERS

The Static Single Assignment Form:

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

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

Lecture 3 Local Optimizations, Intro to SSA

Calvin Lin The University of Texas at Austin

Control flow and loop detection. TDT4205 Lecture 29

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Module 14: Approaches to Control Flow Analysis Lecture 27: Algorithm and Interval. The Lecture Contains: Algorithm to Find Dominators.

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

Compiler Construction 2010/2011 Loop Optimizations

Static Single Assignment Form in the COINS Compiler Infrastructure

Why Global Dataflow Analysis?

Simple and Efficient Construction of Static Single Assignment Form

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

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

Compiler Construction 2016/2017 Loop Optimizations

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

(2,4) Trees. 2/22/2006 (2,4) Trees 1

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

Statements or Basic Blocks (Maximal sequence of code with branching only allowed at end) Possible transfer of control

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

Data structures for optimizing programs with explicit parallelism

Live Variable Analysis. Work List Iterative Algorithm Rehashed

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Data-flow Analysis - Part 2

Optimizations. Optimization Safety. Optimization Safety CS412/CS413. Introduction to Compilers Tim Teitelbaum

Induction Variable Identification (cont)

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

Goals of Program Optimization (1 of 2)

Combining Analyses, Combining Optimizations - Summary

Data Flow Analysis. CSCE Lecture 9-02/15/2018

Control Flow Analysis

Constant Propagation

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

CS 406/534 Compiler Construction Putting It All Together

Using Static Single Assignment Form

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

EECS 583 Class 8 Classic Optimization

Incremental Computation of Dominator Trees

CONTROL FLOW ANALYSIS. The slides adapted from Vikram Adve

CS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017

Intermediate Representation (IR)

Control Flow Analysis. Reading & Topics. Optimization Overview CS2210. Muchnick: chapter 7

Backtracking. Chapter 5

SSI Properties Revisited

Control-Flow Graphs & Dataflow Analysis. CS4410: Spring 2013

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

Search Trees. Undirected graph Directed graph Tree Binary search tree

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

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

Lecture Notes on Common Subexpression Elimination

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Transcription:

Advanced Compilers CMPSCI 70 Spring 00 Computing SSA Emery Berger University of Massachusetts, Amherst More SSA Last time dominance SSA form Today Computing SSA form Criteria for Inserting f Functions Path Convergence Criterion [Cytron-Ferrante ] Brute-force: Insert one φ function for each variable at every join point (point in CFG with more than one predecessor) Wasteful When should we insert φ function for a variable a at node z of the CFG? Intuitively: add φ if there are two definitions of a that can reach the point z through distinct paths Insert φ function for variable a at node z if all the following conditions are true:. There is a block x that defines a. There is a block y x that defines a. There are non-empty paths xfiz and yfiz. Paths xfiz and yfiz don t have nodes in common other than z. The node z does not appear within both xfiz and yfiz prior to the end, but it may appear in one or the other. Note: The start node contains an implicit definition of every variable. Iterated Path-Convergence Criterion Dominance Reviewed 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 - and z does not contain a φ function for a do inserta φ(a 0, a,, a n ) at node z This algorithm is extremely costly, because it requires the examination of every triple of nodes x, y, z and every path from x to z and from y to z. Node x dominates node w if every path from the start node to w must go through x Node x strictly dominates node w if x dominates w and x w Can we do better? 6

Dominance Property of SSA Form Example In SSA form, definitions dominate uses: If x is used in φ function in block n def of x dominates every predecessor of n If x is used in non-φ statement in block n def of x dominates n The dominance frontier of node x: 0 nodes w such that x dominates predecessor of w, but x does not strictly dominate w What is the dominance frontier of node? 7 Example Example 0 0 First we must find all nodes that node strictly dominates. A node w is in the dominance frontier of node if dominates a predecessor of w, but does not strictly dominatew itself. What is the dominance frontier of? 0 Example Dominance Frontier Criterion 0 Dominance Frontier Criterion: If a node x contains a definition of variable a, then any node z in the dominance frontier of x needs a φ function for a. DF() =,,, Can you think of an intuitive explanation for why a node in the dominance frontier of another node must be a join node? A node w is in the dominance frontier of node if dominates a predecessor of w, but does not strictly dominates w itself. What is the dominance frontier of?

Example Dominator Tree 0 If a node () is in the dominance frontier of another node (), than there must be at least two paths converging to (). These paths must be non-intersecting, and one of them (,7,) must contain a node strictly dominated by (). To compute the dominance frontiers, we first compute the dominator tree of the CFG. There is an edge from node x to node y in the dominator tree if node x immediately dominates node y. I.e., x dominates y x, and x does not dominate any other dominator of y. Dominator trees can be computed using the Lengauer-Tarjan algorithm in O(E α(e,n)) time Dominator Tree Local Dominance Frontier Dominator Tree Cytron-Ferrante define the local dominance frontier of a node n as: 0 [n] = successors of n in the CFG that are not strictly dominated by n 0 6 Local Dominance Frontier Dominance Frontier Inherited From Its Children 0 In the example, what are the local dominance frontiers of nodes, 6 and 7? [] = [6] =, [7] =, The dominance frontier of a node n is formed by its local dominance frontier plus nodes that are passed up by the children of n in the dominator tree. The contribution of a node c to its parents dominance frontier is defined as [Cytron-Ferrante, ]: [c] = nodes in the dominance frontier of c that are not strictly dominated by the immediate dominator of c [n] = successors of n in the CFG not strictly dominated by n 7

Up Dominance Frontier 0 In the example, what are the contributions of nodes 6, 7, and to its parent dominance frontier? First we compute the DF and immediate dominator of each node: DF[6] =,, idom(6)= DF[7] =,, idom(7)= DF[] =,, idom()= Up Dominance Frontier 0 First, we compute the DF and the immediate dominator of each node: DF[6] =,, idom(6)= DF[7] =,, idom(7)= DF[] =,, idom()= Now, we check for the condition: [6] = [7] = [] =, [c] = nodes in DF[c] not strictly dominated by idom(c) 0 Dominance Frontier Inherited From Its Children The dominance frontier of a node n is formed by its local dominance frontier plus nodes that are passed up by the children of n in the dominator tree. Thus the dominance frontier of a node n is defined as [Cytron-Ferrante, ]: DF [ n] = DF [ n] local [ ] c DTchildrenn DF up [ c] Local Dominance Frontier 0 What is DF[]? Remember that: [] = [6] = [7] = [] =, DTchildren[] = 6,7, Local Dominance Frontier 0 What is DF[]? Remember that: [] = [6] = [7] = [] =, DTchildren[] = 6,7, Thus, DF[] =,,, Computing Dominance Frontiers Use and for X bottom-up traversal of dominance tree DF(X) Ã for Y Succ(X) // local if idom(y) X then DF(X) Ã DF(X) [ Y for Z Children(X) for Y DF(Z) // up if idom(y) X then DF(X) Ã DF(X) [ Y

Join Sets Iterated Join Sets In order to insert φ-nodes for a variable x that is defined in a set of nodes S=n, n,, n k we need to compute the iterated set of join nodes of S. Given a set of nodes S of a control flow graph G, the set of join nodes of S, J(S), is defined as follows: J(S) =z G two paths P xz and P yz in G that have z as its first common node, x S and y S Because a φ-node is itself a definition of a variable, once we insert φ-nodes in the join set of S, we need to find out the join set of S J(S). Thus, Cytron-Ferrante define the iterated join set of a set of nodes S, J + (S), as the limit of the sequence: J = J J = ( S) J( S ) i+ J i 6 Iterated Dominance Frontier We can extend the concept of dominance frontier to define the dominance frontier of a set of nodes as: DF( S) = Υ DF( X) X S Now we can define the iterated dominance frontier, DF + (S), of a set of nodes S as the limit of the sequence: DF = DF( S DF = DF ) ( S ) i DF + i Location of f-nodes Given a variable x that is defined in a set of nodes S=n, n,, n k the set of nodes that must receive φ-nodes for x is J + (S). An important result proved by Cytron-Ferrante is that: J + ( S) = DF ( S) + Thus we are really interested in computing the iterated dominance frontier of a set of nodes. 7 Algorithms to Compute F Node Placement The algorithm to insert φ-nodes, due to Cytron and Ferrante (), computes the dominance frontier of each node in the set S before computing the iterated dominance frontier of the set. In the worst case, the combination of the dominance frontier of the sets can be quadratic in the number of nodes in the CFG. Thus, Cytron-Ferrante s algorithm has a complexity O(N ). In, Shreedar and Gao proposed a simple, linear algorithm for the insertion of φ-nodes. Sreedhar and Gao s DJ Graph 0 Dominator Tree 0 0

Sreedhar and Gao s DJ Graph Sreedhar and Gao s DJ Graph D nodes Dominator Tree D nodes J nodes Dominator Tree 0 0 0 0 Shreedar-Gao s Dominance Shreedar-Gao s Dominance : foreach y SubTree(x) do : if((y z == J-edge) and : (z.level x.level)) : then DF[x] = DF[x] z : foreach y SubTree(x) do : if((y z == J-edge) and : (z.level x.level)) : then DF[x] = DF[x] z Initialization: DF[] = SubTree() =, 6, 7, What is the DF[]? 0 0 Shreedar-Gao s Dominance Shreedar-Gao s Dominance : foreach y SubTree(x) do : if((y z == J-edge) and : (z.level x.level)) : then DF[x] = DF[x] z Initialization: DF[] = : foreach y SubTree(x) do : if((y z == J-edge) and : (z.level x.level)) : then DF[x] = DF[x] z Initialization: DF[] = After visiting 6: DF = SubTree() =, 6, 7, SubTree() =, 6, 7, There are three edges originating in : 6, 7, but they are all D-edges 0 There are two edges originating in 6: 6, 6 but.level >.level 0 6 6

Shreedar-Gao s Dominance : foreach y SubTree(x) do : if((y z == J-edge) and : (z.level x.level)) : then DF[x] = DF[x] z Initialization: DF[] = After visiting 6: DF = After visiting 7: DF =, Shreedar-Gao s Dominance : foreach y SubTree(x) do : if((y z == J-edge) and : (z.level x.level)) : then DF[x] = DF[x] z Initialization: DF[] = After visiting 6: DF = After visiting 7: DF =, After visiting : DF =,,, SubTree() =, 6, 7, SubTree() =, 6, 7, There are two edges originating in 7: 7, 7 again.level >.level 0 There are two edges originating in :, both satisfy cond. in steps - 0 7 Shreedhar-Gao s f-node Insertion Algorithm Shreedhar-Gao s f-node Insertion Algorithm Using the D-J graph, Shreedhar and Gao propose a linear time algorithm to compute the iterated dominance frontier of a set of nodes. A important intuition in Shreedhar-Gao s algorithm is: Shreedhar-Gao s algorithm also use a work list of nodes hashed by their level in the dominator tree and a visitedflag to avoid visiting the same node more than once. If two nodes x and y are in S, and y is an ancestor of x in the dominator tree, than if we compute DF[x] first, we do not need to recompute it when computing DF[y]. The basic operation of the algorithm is similar to their dominance frontier algorithm, but it requires a careful implementation to deliver the linear time complexity. 0 Dead-Code Elimination in SSA Form Simple Constant Propagation in SSA Only one definition for each variable ) if the list of uses of the variable is empty, 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, which might cause those definitions to become dead. Thus we need to iterate the dead code elimination algorithm. 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 f(c, c,, c n ) where all c i 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 In the next slide we assume that x, y, z are variables and a, b, c are constants. 7

Linear Time Optimizations in SSA form Copy propagation: The statement x f(y) or the statement x ycan be deleted and y can substitute every use of x. Next Time Implementing other optimizations with SSA Constant folding: If we have the statement x a b, we can evaluate c a b at compile time and replace the statement for x c Constant conditions: The conditional if a < b gotol L can be replaced for gotol or goto L, according to the compile time evaluation of a < b, and the CFG, use lists, adjust accordingly Unreachable Code: eliminate unreachable blocks. Single Assignment Form i=; j=; while(k<00) if(j<0) k=k+; k=k+; B j i k k+ B B B if j<0 i j k 0 if k<00 return j B j k k k+ Single Assignment Form i=; j=; while(k<00) if(j<0) k=k+; k=k+; B B if j<0 j i k k+ B B i j k 0 if k<00 return j B j k k k+ 6 Single Assignment Form i=; j=; while(k<00) if(j<0) k=k+; k=k+; B j i k k+ B B if j<0 i j k 0 B if k<00 k φ(k,k) return j B j k k k+ Single Assignment Form i=; j=; while(k<00) if(j<0) k=k+; k=k+; B j i k k+ B B B if j<0 i j k 0 k φ(k,k) if k<00 k φ(k,k) return j B j k k k+ 7

Single Assignment Form i=; j=; while(k<00) if(j<0) k=k+; k=k+; B B B if j<0 j i k k+ B i j k 0 k φ(k,k) if k<00 return j B j k k k+ k φ(k,k) Single Assignment Form i=; j=; while(k<00) if(j<0) k=k+; k=k+; B B if j<0 j i k k+ B j φ(j,j) k φ(k,k) B i j k 0 j φ(j,j) k φ(k,k) if k<00 j k k k+ 0 Constant Propagation B i j k 0 B i j k 0 Dead-code Elimination B i j k 0 B j φ(j,j) k φ(k,k) if k<00 B j φ(j,) k φ(k,0) if k<00 B j φ(j,) k φ(k,0) if k<00 B j φ(j,) k φ(k,0) if k<00 B if j<0 B if j<0 B if j<0 B if j<0 B j i k k+ j k k k+ B j k k+ j k k k+ B j k k+ j k k k+ B j k k+ j k k k+ j φ(j,j) k φ(k,k) j φ(j,j) k φ(k,k) j φ(j,j) k φ(k,k) j φ(j,j) k φ(k,k) B j k k+ Constant Propagation and Dead Code Elimination B B if j<0 j φ(j,j) k φ(k,k) j φ(j,) k φ(k,0) if k<00 j k k k+ B B if j<0 j k k+ B j φ(,j) k φ(k,k) j φ(j,) k φ(k,0) if k<00 j k k k+ Is this the end? B k k+ B j φ(j,) k φ(k,0) if k<00 B if j<0 j φ(,j) k φ(k,k) j k k k+ But block 6 is never executed! How can we find this out, and simplify the program? SSA conditional constant propagation finds the least fixed point for the program and allows further elimination of dead code.

Dead code elimination Single Argument f- Function Elimination B j φ(j,) k φ(k,0) if k<00 B j φ(j,) k φ(k,0) if k<00 B j φ(j,) k φ(k,0) if k<00 B j φ(j,) k φ(k,0) if k<00 B if j<0 B k k+ j k k k+ B k k+ B k k+ B k k+ j φ(,j) k φ(k,k) j φ() k φ(k) j φ() k φ(k) j k k 6 Constant and Copy Propagation Dead Code Elimination B j φ(j,) k φ(k,0) if k<00 B j φ(,) k φ(k,0) if k<00 B j φ(,) k φ(k,0) if k<00 B j φ(,) k φ(k,0) if k<00 B k k+ B k k+ B k k+ B k k+ j k k j k k j k k 7 f-function Simplification Constant Propagation B j φ(,) k φ(k,0) if k<00 B j k φ(k,0) if k<00 B j k φ(k,0) if k<00 B j k φ(k,0) if k<00 return B B k k+ B k k+ B k k+ B k k+ 60 0

Dead Code Elimination Next Time B j k φ(k,0) if k<00 return B return B B k k+ 6 6