CS745: SSA© Seth Copen Goldstein & Todd C. Mowry 2001-31 15-745 Static Single Assignment.

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.
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 Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
- 1 - Dominator Tree BB0 BB1 BB2BB3 BB4 BB6 BB7 BB5 BB0 BB1 BB2BB3 BB4 BB6 BB5 BB7 BBDOM0 10,1 20,1,2 30,1,3 BBDOM 40,1,3,4 50,1,3,5 60,1,3,6 70,1,7 Dom.
SSA.
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.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Computer Science 313 – Advanced Programming Topics.
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.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
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.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
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.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
Lecture #17, June 5, 2007 Static Single Assignment phi nodes Dominators Dominance Frontiers Dominance Frontiers when inserting phi nodes.
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.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
Projects. Dataflow analysis Dataflow analysis: what is it? A common framework for expressing algorithms that compute information about a program Why.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
CS745: Register Allocation© Seth Copen Goldstein & Todd C. Mowry Register Allocation.
Precision Going back to constant prop, in what cases would we lose precision?
CSE P501 – Compiler Construction
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Static Single Assignment.
Interference Graphs for Programs in Static Single Information Form are Interval Graphs Philip Brisk Processor Architecture Laboratory (LAP) EPFL Lausanne,
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.
Λλ 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.
Building SSA Form (A mildly abridged account) For the full story, see the lecture notes for COMP 512 (lecture 8) and.
Program Representations. Representing programs Goals.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
Introduction to SSA Data-flow Analysis Revisited – Static Single Assignment (SSA) Form Liberally Borrowed from U. Delaware and Cooper and Torczon Text.
Computer Science 313 – Advanced Programming Topics.
Building SSA Form, I 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at.
Dataflow analysis.
Static Single Assignment
© Seth Copen Goldstein & Todd C. Mowry
Efficiently Computing SSA
Factored Use-Def Chains and Static Single Assignment Forms
Building SSA Form COMP 512 Rice University Houston, Texas Fall 2003
CS 201 Compiler Construction
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Static Single Assignment Form (SSA)
Optimizations using SSA
Dominator Tree First BB is the root node, each node
Data Flow Analysis Compiler Design
EECS 583 – Class 7 Static Single Assignment Form
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
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.
EECS 583 – Class 7 Static Single Assignment Form
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
Presentation transcript:

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Static Single Assignment

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Values  Locations … for (i=0; i++; i<10) { … = … i …; … } for (i=j; i++; i<20) { … = i … }

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Values  Locations … for (i=0; i++; i<10) { … = … i …; … } for (i=j; i++; i<20) { … = i … } Def-use chains help solve the problem.

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Def-Use chains are expensive foo(int i, int j) { … switch (i) { case 0: x=3;break; case 1: x=1; break; case 2: x=6; break; case 3: x=7; break; default: x = 11; } switch (j) { case 0: y=x+7; break; case 1: y=x+4; break; case 2: y=x-2; break; case 3: y=x+1; break; default: y=x+9; } …

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Def-Use chains are expensive foo(int i, int j) { … switch (i) { case 0: x=3; case 1: x=1; case 2: x=6; case 3: x=7; default: x = 11; } switch (j) { case 0: y=x+7; case 1: y=x+4; case 2: y=x-2; case 3: y=x+1; default: y=x+9; } … In general, N defs M uses  O(NM) space and time A solution is to limit each var to ONE def site

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Def-Use chains are expensive foo(int i, int j) { … switch (i) { case 0: x=3; break; case 1: x=1; break; case 2: x=6; case 3: x=7; default: x = 11; } x1 is one of the above x’s switch (j) { case 0: y=x1+7; case 1: y=x1+4; case 2: y=x1-2; case 3: y=x1+1; default: y=x1+9; } … A solution is to limit each var to ONE def site

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Advantages of SSA Makes du-chains explicit Makes dataflow analysis easier Improves register allocation –Automatically builds Webs –Makes building interference graphs easier For most programs reduces space/time requirements

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry SSA Static single assignment is an IR where every variable is assigned a value at most once in the program text Easy for a basic block: –assign to a fresh variable at each stmt. –each use uses the most recently defined var. –(Similar to Value Numbering)

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Straight-line SSA a  x + y b  a + x a  b + 2 c  y + 1 a  c + a

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Straight-line SSA a  x + y b  a + x a  b + 2 c  y + 1 a  c + a a 1  x + y b 1  a 1 + x a 2  b c 1  y + 1 a 3  c 1 + a 2

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry SSA Static single assignment is an IR where every variable is assigned a value at most once in the program text Easy for a basic block: –assign to a fresh variable at each stmt. –each use uses the most recently defined var. –(Similar to Value Numbering) What about at joins in the CFG?

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry a 1  x + y b 1  a 1 + x Merging at Joins a  b + 2 c  y + 1 c  12 if (i) { a  x + y b  a + x } else { a  b + 2 c  y + 1 } a  c + a c 1  12 if (i) a 4  c ? + a ?

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry SSA Static single assignment is an IR where every variable is assigned a value at most once in the program text Easy for a basic block: –assign to a fresh variable at each stmt. –Each use uses the most recently defined var. –(Similar to Value Numbering) What about at joins in the CFG? –Use a notional fiction: A  function

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Merging at Joins a 2  b + 2 c 2  y + 1 c 1  12 if (i) a 1  x + y b 1  a 1 + x a 3   (a 1,a 2 ) c 3   (c 1,c 2 ) b 2   (b 1, ?) a 4  c 3 + a 3

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry The  function  merges multiple definitions along multiple control paths into a single definition. At a BB with p predecessors, there are p arguments to the  function. x new   (x 1, x 1, x 1, …, x p ) How do we choose which x i to use? –We don’t really care! –If we care, use moves on each incoming edge

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry “Implementing”  a 2  b + 2 c 2  y + 1 a 3  a 2 c 3  c 2 c 1  12 if (i) a 1  x + y b 1  a 1 + x a 3  a 1 c 3  c 1 a 3   (a 1,a 2 ) c 3   (c 1,c 2 ) a 4  c 3 + a 3

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Trivial SSA Each assignment generates a fresh variable. At each join point insert  functions for all live variables. y  x y  2 z  y + x x  1 y 1  x 1 y 2  2 x 2   (x 1,x 1 ) y 3   (y 1,y 2 ) z 1  y 3 + x 2 x 1  1 Way too many  functions inserted.

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Minimal SSA Each assignment generates a fresh variable. At each join point insert  functions for all variables with multiple outstanding defs. y  x y  2 z  y + x x  1 y 1  x 1 y 2  2 y 3   (y 1,y 2 ) z 1  y 3 + x 1 x 1  1

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Another Example a  0 b  a + 1 c  c + b a  b * 2 if a < N return c

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Another Example a  0 b  a + 1 c  c + b a  b * 2 if a < N return c a 1  0 a 3   (a 1,a 2 ) c 3   (c 1,c 2 ) b 2  a c 2  c 3 + b 2 a 2  b 2 * 2 if a 2 < N return c 2 Notice use of c 1

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry When do we insert  ? CFG If there is a def of a in block 5, which nodes need a  ()?

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry When do we insert  ? We insert a  function for variable A in block Z iff: –A was defined more than once before (i.e., A defined in X and Y AND X  Y) –There exists a non-empty path from x to z, P xz, and a non-empty from y to z, P yz s.t. P xz  P yz = { z } z  P xq or z  P yr where P xz = P xq  z and P yz = P yr  z Entry block contains an implicit def of all vars Note: A =  (…) is a def of A

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Dominance Property of SSA In SSA, definitions dominate uses. –If x i is used in x   (…, x i, …), then BB(x i ) dominates i th predecessor of BB(PHI) –If x is used in y  … x …, then BB(x) dominates BB(y) We can use this for an efficient algorithm to convert to SSA

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Dominance CFGD-Tree x strictly dominates w (x sdom w) iff x dom w AND x  w If there is a def of a in block 5, which nodes need a  ()?

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Dominance Frontier CFGD-Tree x strictly dominates w (x sdom w) iff x dom w AND x  w The dominance Frontier of a node x = { w | x dom pred(w) AND !(x sdom w)}

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Dominance Frontier & path-convergence

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Using DF to compute SSA place all  () Rename all variables

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Using DF to Place  () Gather all the defsites of every variable Then, for every variable –foreach defsite foreach node in DF(defsite) –if we haven’t put  () in node put one in –If this node didn’t define the variable before: add this node to the defsites This essentially computes the Iterated Dominance Frontier on the fly, inserting the minimal number of  () neccesary

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Using DF to Place  () foreach node n { foreach variable v defined in n { orig[n]  = {v} defsites[v]  = {n} } foreach variable v { W = defsites[v] while W not empty { n = remove node from W foreach y in DF[n] if y  PHI[v] { insert “v   (v,v,…)” at top of y PHI[v] = PHI[v]  {y} if v  orig[y]: W = W  {y} }

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Renaming Variables Walk the D-tree, renaming variables as you go Replace uses with more recent renamed def –For straight-line code this is easy –If there are branches and joins?

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Renaming Variables Walk the D-tree, renaming variables as you go Replace uses with more recent renamed def –For straight-line code this is easy –If there are branches and joins use the closest def such that the def is above the use in the D-tree Easy implementation: –for each var: rename (v) –rename(v):replace uses with top of stack at def: push onto stack call rename(v) on all children in D-tree for each def in this block pop from stack

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Compute D-tree i  1 j  1 k  0 k < 100? j < 20?return j j  i k  k + 1 j  k k  k

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Compute D-tree i  1 j  1 k  0 k < 100? j < 20?return j j  i k  k + 1 j  k k  k D-tree 3

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Compute Dominance Frontier i  1 j  1 k  0 k < 100? j < 20?return j j  i k  k + 1 j  k k  k DFs 1{} 2{2} 3{2} 4{} 5{7} 6{7} 7{2} 3

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Insert  () i  1 j  1 k  0 k < 100? j < 20?return j j  i k  k + 1 j  k k  k DFs 1{} 2{2} 3{2} 4{} 5{7} 6{7} 7{2} 3 1{ i,j,k} 2{} 3{} 4{} 5{j,k} 6{j,k} 7{} orig[n] defsites[v] i{1} j{1,5,6} k{1,5,6} var i: W={1} var j: W={1,5,6} DF{1}, DF{5}

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Insert  () i  1 j  1 k  0 k < 100? j < 20?return j j  i k  k + 1 j  k k  k + 2 j   (j,j) DFs 1{} 2{2} 3{2} 4{} 5{7} 6{7} 7{2} 3 1{ i,j,k} 2{} 3{} 4{} 5{j,k} 6{j,k} 7{} orig[n] defsites[v] i{1} j{1,5,6} k{1,5,6} var j: W={1,5,6} DF{1}, DF{5}

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Insert  () i  1 j  1 k  0 j   (j,j) k < 100? j < 20?return j j  i k  k + 1 j  k k  k + 2 j   (j,j) DFs 1{} 2{2} 3{2} 4{} 5{7} 6{7} 7{2} 3 1{ i,j,k} 2{} 3{} 4{} 5{j,k} 6{j,k} 7{} orig[n] defsites[v] i{1} j{1,5,6} k{1,5,6} var j: W={1,5,6} DF{1}, DF{5}

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Insert  () i  1 j  1 k  0 j   (j,j) k < 100? j < 20?return j j  i k  k + 1 j  k k  k + 2 j   (j,j) DFs 1{} 2{2} 3{2} 4{} 5{7} 6{7} 7{2} 3 1{ i,j,k} 2{} 3{} 4{} 5{j,k} 6{j,k} 7{} orig[n] defsites[v] i{1} j{1,5,6} k{1,5,6} var j: W={1,5,6} DF{1}, DF{5}, DF{6}

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Insert  () i  1 j  1 k  0 j   (j,j) k   (k,k) k < 100? j < 20?return j j  i k  k + 1 j  k k  k + 2 j   (j,j) k   (k,k) DFs 1{} 2{2} 3{2} 4{} 5{7} 6{7} 7{2} 3 1{ i,j,k} 2{} 3{} 4{} 5{j,k} 6{j,k} 7{} orig[n] defsites[v] i{1} j{1,5,6} k{1,5,6} var k: W={1,5,6}

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Rename Vars i 1  1 j 1  1 k  0 j 2   (j,j 1 ) k   (k,k) k < 100? j < 20?return j j  i 1 k  k + 1 j  k k  k + 2 j   (j,j) k   (k,k)

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Rename Vars i 1  1 j 1  1 k 1  0 j 2   (j 4,j 1 ) k 2   (k 4,k 1 ) k 2 < 100? j 2 < 20?return j 2 j 3  i 1 k 3  k j 5  k 2 k 5  k j 4   (j 3,j 5 ) k 4   (k 3,k 5 )

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Computing DF(n) n a b c n dom a n dom b !n dom c

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Computing DF(n) n a b c DF(a) DF(b) x n dom a n dom b !n dom c

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Computing the Dominance Frontier compute-DF(n) S = {} foreach node y in succ[n] if idom(y)  n S = S  { y } foreach child of n, c, in D-tree compute-DF(c) foreach w in DF[c] if !n dom w S = S  { w } DF[n] = S The dominance Frontier of a node x = { w | x dom pred(w) AND !(x sdom w)}

CS745: SSA© Seth Copen Goldstein & Todd C. Mowry SSA Properties Only 1 assignment per variable definitions dominate uses