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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
SSA.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Computer Science 313 – Advanced Programming Topics.
Stanford University CS243 Winter 2006 Wei Li 1 SSA.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
Advanced Compiler Design – Assignment 1 SSA Construction & Destruction Michael Fäs (Original Slides by Luca Della Toffola)
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 More data flow analysis Emery Berger.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Lecture 2 Emery Berger University of.
CS 536 Spring Global Optimizations Lecture 23.
CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Static Single Assignment.
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.
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic G: Static Single- Assignment Form José Nelson Amaral
CS 201 Compiler Construction
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University.
1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
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.
Dominators, control-dependence and SSA form. Organization Dominator relation of CFGs –postdominator relation Dominator tree Computing dominator relation.
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,
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
Generating SSA Form (mostly from Morgan). Why is SSA form useful? For many dataflow problems, SSA form enables sparse dataflow analysis that –yields the.
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.
Control Flow Analysis Compiler Baojian Hua
Introduction to SSA Data-flow Analysis Revisited – Static Single Assignment (SSA) Form Liberally Borrowed from U. Delaware and Cooper and Torczon Text.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment John Cavazos.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Optimizing The Optimizer: Improving Data Flow Analysis in Soot Michael Batchelder 4 / 6 / 2005 A COMP-621 Class Project.
Data Flow Analysis Suman Jana
Static Single Assignment
© Seth Copen Goldstein & Todd C. Mowry
Efficiently Computing SSA
Topic 10: Dataflow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
University Of Virginia
CS 201 Compiler Construction
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Static Single Assignment Form (SSA)
Optimizations using SSA
Data Flow Analysis Compiler Design
Static Single Assignment
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.
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.
SSA-based Optimizations
Building SSA Harry Xu CS 142 (b) 04/22/2018.
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:

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 of Massachusetts, Amherst

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 2 More SSA Last time dominance SSA form Today Computing SSA form

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 3 Criteria for Inserting  Functions 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 4 Path Convergence Criterion [Cytron-Ferrante ‘89] Insert  function for variable a at 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 are non-empty paths x  z and y  z 4.Paths x  z and y  z don’t have nodes in common other than z 5.The node z does not appear within both x  z and y  z prior to the end, but it may appear in one or the other. Note: The start node contains an implicit definition of every variable.

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 5 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 0, a 1, …, 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. Can we do better?

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 6 Dominance Reviewed 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 7 Dominance Property of SSA Form 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: nodes w such that x dominates predecessor of w, but x does not strictly dominate w

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 8 Example What is the dominance frontier of node 5?

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 9 Example First we must find all nodes that node 5 strictly dominates.

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 10 Example A node w is in the dominance frontier of node 5 if 5 dominates a predecessor of w, but 5 does not strictly dominate w itself. What is the dominance frontier of 5?

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 11 Example DF(5) = {4, 5, 12, 13} A node w is in the dominance frontier of node 5 if 5 dominates a predecessor of w, but 5 does not strictly dominates w itself. What is the dominance frontier of 5?

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 12 Dominance Frontier Criterion 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. Can you think of an intuitive explanation for why a node in the dominance frontier of another node must be a join node?

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 13 Example If a node (12) is in the dominance frontier of another node (5), than there must be at least two paths converging to (12). These paths must be non-intersecting, and one of them (5,7,12) must contain a node strictly dominated by (5).

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 14 Dominator Tree 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 15 Example: Dominator Tree Control Flow Graph Dominator Tree

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 16 Local Dominance Frontier Cytron-Ferrante define the local dominance frontier of a node n as: DF local [n] = successors of n in the CFG that are not strictly dominated by n

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 17 Example: Local Dominance Frontier Control Flow Graph In the example, what are the local dominance frontiers of nodes 5, 6 and 7? DF local [5] =  DF local [6] = {4,8} DF local [7] = {8,12} DF local [n] = successors of n in the CFG not strictly dominated by n

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 18 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. The contribution of a node c to its parents’ dominance frontier is defined as [Cytron-Ferrante, 1991]: DF up [c] = nodes in the dominance frontier of c that are not strictly dominated by the immediate dominator of c

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 19 Example: Up Dominance Frontier Control Flow Graph In the example, what are the contributions of nodes 6, 7, and 8 to its parent dominance frontier? First we compute the DF and immediate dominator of each node: DF[6] = {4,8}, idom(6)= 5 DF[7] = {8,12}, idom(7)= 5 DF[8] = {5,13}, idom(8)= 5

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 20 Example: Up Dominance Frontier Control Flow Graph First, we compute the DF and the immediate dominator of each node: DF[6] = {4,8}, idom(6)= 5 DF[7] = {8,12}, idom(7)= 5 DF[8] = {5,13}, idom(8)= 5 Now, we check for the DF up condition: DF up [6] = {4} DF up [7] = {12} DF up [8] = {5,13} DF up [c] = nodes in DF[c] not strictly dominated by idom(c)

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 21 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, 1991]:

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 22 Example: Local Dominance Frontier Control Flow Graph What is DF[5]? Remember that: DF local [5] =  DF up [6] = {4} DF up [7] = {12} DF up [8] = {5,13} DTchildren[5] = {6,7,8}

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 23 Example: Local Dominance Frontier Control Flow Graph What is DF[5]? Remember that: DF local [5] =  DF up [6] = {4} DF up [7] = {12} DF up [8] = {5,13} DTchildren[5] = {6,7,8} Thus, DF[5] = {4, 5, 12, 13}

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 24 Computing Dominance Frontiers Use DF local and DF up for X 2 bottom-up traversal of dominance tree DF(X) Ã  for Y 2 Succ(X) // local if idom(Y)  X then DF(X) Ã DF(X) [ {Y} for Z 2 Children(X) for Y 2 DF(Z) // up if idom(Y)  X then DF(X) Ã DF(X) [ {Y}

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 25 Join Sets In order to insert  -nodes for a variable x that is defined in a set of nodes S={n 1, n 2, …, 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}

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 26 Iterated Join Sets 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:

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 27 Iterated Dominance Frontier We can extend the concept of dominance frontier to define the dominance frontier of a set of nodes as: Now we can define the iterated dominance frontier, DF + (S), of a set of nodes S as the limit of the sequence: Jose Nelson Amaral: Find an example in which the IDF of a set S is different from the DF of the set! Jose Nelson Amaral: Find an example in which the IDF of a set S is different from the DF of the set!

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 28 Location of  -Nodes Given a variable x that is defined in a set of nodes S={n 1, n 2, …, n k } the set of nodes that must receive  -nodes for x is J + (S). An important result proved by Cytron-Ferrante is that: Thus we are really interested in computing the iterated dominance frontier of a set of nodes.

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 29 Algorithms to Compute  Node Placement The algorithm to insert  -nodes, due to Cytron and Ferrante (1991), computes the dominance frontier of each node in the set S before computing the iterated dominance frontier of the set. In 1994, Shreedar and Gao proposed a simple, linear algorithm for the insertion of  -nodes. 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 2 ).

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 30 Sreedhar and Gao’s DJ Graph Control Flow Graph Dominator Tree

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 31 Sreedhar and Gao’s DJ Graph Control Flow Graph Dominator Tree D nodes

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 32 Sreedhar and Gao’s DJ Graph Control Flow Graph Dominator Tree D nodes J nodes

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 33 Shreedar-Gao’s Dominance Frontier Algorithm DominanceFrontier(x) 0: DF[x] =  1: foreach y  SubTree(x) do 2: if((y  z == J-edge) and 3: (z.level  x.level)) 4: then DF[x] = DF[x]  z What is the DF[5]?

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 34 Shreedar-Gao’s Dominance Frontier Algorithm DominanceFrontier(x) 0: DF[x] =  1: foreach y  SubTree(x) do 2: if((y  z == J-edge) and 3: (z.level  x.level)) 4: then DF[x] = DF[x]  z SubTree(5) = {5, 6, 7, 8} Initialization: DF[5] = 

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 35 Shreedar-Gao’s Dominance Frontier Algorithm DominanceFrontier(x) 0: DF[x] =  1: foreach y  SubTree(x) do 2: if((y  z == J-edge) and 3: (z.level  x.level)) 4: then DF[x] = DF[x]  z SubTree(5) = {5, 6, 7, 8} There are three edges originating in 5: {5  6, 5  7, 5  8} but they are all D-edges Initialization: DF[5] = 

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 36 Shreedar-Gao’s Dominance Frontier Algorithm DominanceFrontier(x) 0: DF[x] =  1: foreach y  SubTree(x) do 2: if((y  z == J-edge) and 3: (z.level  x.level)) 4: then DF[x] = DF[x]  z SubTree(5) = {5, 6, 7, 8} There are two edges originating in 6: {6  4, 6  8} but 8.level > 5.level Initialization: DF[5] =  After visiting 6: DF = {4}

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 37 Shreedar-Gao’s Dominance Frontier Algorithm DominanceFrontier(x) 0: DF[x] =  1: foreach y  SubTree(x) do 2: if((y  z == J-edge) and 3: (z.level  x.level)) 4: then DF[x] = DF[x]  z SubTree(5) = {5, 6, 7, 8} There are two edges originating in 7: {7  8, 7  12} again 8.level > 5.level Initialization: DF[5] =  After visiting 6: DF = {4} After visiting 7: DF = {4,12}

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 38 Shreedar-Gao’s Dominance Frontier Algorithm DominanceFrontier(x) 0: DF[x] =  1: foreach y  SubTree(x) do 2: if((y  z == J-edge) and 3: (z.level  x.level)) 4: then DF[x] = DF[x]  z SubTree(5) = {5, 6, 7, 8} There are two edges originating in 8: {8  5, 8  13} both satisfy cond. in steps 2-3 Initialization: DF[5] =  After visiting 6: DF = {4} After visiting 7: DF = {4,12} After visiting 8: DF = {4, 12, 5, 13}

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 39 Shreedhar-Gao’s  -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: 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].

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 40 Shreedhar-Gao’s  -Node Insertion Algorithm Shreedhar-Gao’s algorithm also use a work list of nodes hashed by their level in the dominator tree and a visited flag to avoid visiting the same node more than once. 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.

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 41 Dead-Code Elimination in SSA Form 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.

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 42 Simple Constant Propagation in SSA 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   (c 1, c 2, …, 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.

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 43 Linear Time Optimizations in SSA form Copy propagation: The statement x   (y) or the statement x  y can be deleted and y can substitute every use of x. 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 goto L1 else L2 can be replaced for goto L1 or goto L2, according to the compile time evaluation of a < b, and the CFG, use lists, adjust accordingly Unreachable Code: eliminate unreachable blocks.

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 44 Next Time Implementing other optimizations with SSA

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 45 Single Assignment Form 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; } i  1 j  1 k  0 j  i k  k+1 j  k k  k+2 return j if j<20 if k<100 B1 B2 B3 B5 B6 B4 B7

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 46 Single Assignment Form 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; } i  1 j  1 k1  0 j  i k3  k+1 j  k k5  k+2 return j if j<20 if k<100 B1 B2 B3 B5 B6 B4 B7

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 47 Single Assignment Form 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; } i  1 j  1 k1  0 j  i k3  k+1 j  k k5  k+2 return j if j<20 if k<100 k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 48 Single Assignment Form 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; } i  1 j  1 k1  0 j  i k3  k+1 j  k k5  k+2 return j if j<20 k2   (k4,k1) if k<100 k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 49 Single Assignment Form 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; } i  1 j  1 k1  0 j  i k3  k2+1 j  k k5  k2+2 return j if j<20 k2   (k4,k1) if k2<100 k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 50 Single Assignment Form 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; } 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 51 Example: Constant Propagation 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 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 52 Example: Dead-code Elimination 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 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 53 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   (1,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   (j3,j5) k4   (k3,k5) B2 B3 B5 B6 B4 B7

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 54 Example: Is this the end? 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. k3  k2+1j5  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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 55 Example: Dead code elimination k3  k2+1j5  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 B4 k3  k2+1 return j2 j2   (j4,1) k2   (k4,0) if k2<100 j4   (1) k4   (k3) B2 B5 B7

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 56 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 B4 k3  k2+1 return j2 j2   (j4,1) k2   (k4,0) if k2<100 j4  1 k4  k3 B2 B5 B7 B4

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 57 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 58 Example: Dead Code Elimination k3  k2+1 return j2 j2   (1,1) k2   (k3,0) if k2<100 j4  1 k4  k3 B2 B5 B7 B4 k3  k2+1 return j2 j2   (1,1) k2   (k3,0) if k2<100 B2 B5 B4

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 59 Example:  -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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 60 Example: 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 61 Example: Dead Code Elimination k3  k2+1 return 1 j2  1 k2   (k3,0) if k2<100 B2 B5 B4 return 1 B4

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 62 Next Time