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.

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

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Static Single Assignment (SSA) Form Jaeho Shin :00 ROPAS Weekly Show & Tell.
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 - 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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
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 Constant Propagation for Loops with Factored Use-Def Chains Reporter : Lai, Yen-Chang.
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.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
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.
CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Static Single Assignment.
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
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.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
CSE P501 – Compiler Construction
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Static Single Assignment.
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.
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.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment John Cavazos.
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.
Dominators and CFGs Taken largely from University of Delaware Compiler Notes.
Loops Simone Campanoni
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
CS 201 Compiler Construction
Trees ---- Soujanya.
© Seth Copen Goldstein & Todd C. Mowry
Building SSA Form (A mildly abridged account)
Efficiently Computing SSA
CS 201 Compiler Construction
More Graph Algorithms.
Factored Use-Def Chains and Static Single Assignment Forms
Taken largely from University of Delaware Compiler Notes
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
EECS 583 – Class 7 Static Single Assignment Form
COMP171 Depth-First Search.
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
EECS 583 – Class 8 Finish SSA, Start on Classic Optimization
EECS 583 – Class 7 Static Single Assignment Form
Compiler Construction
Taken largely from University of Delaware Compiler Notes
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:

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 flow paths meet. To find where  -functions should be placed, we compute the Dominance Frontier of each BB Intuition: The nodes in the dominance frontier of B are destinations of the edges that leave an area dominated by B. Since those destinations are not dominated by B, there must be some other path from the entry to them. Therefore, they must be convergence points, and we need to place  functions in them for any variables defined in B.

2 Dominance frontier algorithm compute the dominator tree for each X in a bottom-up traversal of the tree, do DF(X) := { } for each Y  Succ (X) do if (idom (Y)  X) then DF(X) := DF(X)  {Y} end for for each Z in children(X) do for each W  DF(Z) do if (idom (W)  X) then DF(X) := DF(X)  {Y} end for DF local  DF up

3 Inserting  -functions Intuition: If there is a definition for x in a basic block B, insert a  - function in each basic block in DF(B). The  -function should have as many arguments as the number of B's predecessors Algorithm sketch: Traverse all BBs and, for each variable x, note which BBs contain a definition of x. For each variable x Pick a BB n where x is defined Insert a  -function for x in each m in DF(n) (unless it already contains one) Add m in the list of BBs where x is defined.

4 Renaming variables in  -functions When the  -functions are inserted, the variables involved are not named (subscripted). This is done at the next stage. Basics: The variables will be renamed by traversing the dominator tree in pre-order. We will use a stack for each variable, to keep track of the most recent definition After uses and definitions in a BB have been renamed, recurse on its children We pop the stack after we've been through the children of a BB in the dominator tree

5 Renaming variables in  -functions for each global name i counter[i]  0 stack[i]  Ø call Rename(B 0 ) NewName(n) i  counter[n] counter[n]  counter[n] + 1 push n i onto stack[n] return n i Rename(b) for each  -function in b, x   (…) rename x as NewName(x) for each operation “x  y op z” in b rewrite y as top(stack[y]) rewrite z as top(stack[z]) rewrite x as NewName(x) for each successor of b in the CFG rewrite appropriate  parameters for each child c of b in dom. tree Rename(c) for each operation “x  y op z” in b pop(stack[x]) counter: keeps track of #defs stack: keeps track of subscript

6 read(n) a := 1 b := 1 n <= 1 i := 2 i<=n a := n b := b+1 a == b a := b+1 b := n return n B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} i:=i+1 B7 {B3} a b i defined in B1, B4, B5 nB1 B1, B4, B5 B2, B7  -functions in B7, B3, B8 B8, B3 usually, we insert a predecessor to exit and place the functions there exit B8

7 read(n) a := 1 b := 1 n <= 1 i := 2 i:=  (i,i) a:=  (a,a) b:=  (b,b) i<=n a := n b := b+1 a == b a := b+1 b := n return n a:=  (a,a) b:=  (b,b) i:=  (i,i) B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} a:=  (a,a) b:=  (b,b) i:=i+1 B7 {B3} a b i defined in B1, B4, B5 nB1 B1, B4, B5 B2, B7  -functions in B7, B3, B8 B8, B3 exit B8

8 read(n0) a0 := 1 b0 := 1 n0 <= 1 i := 2 i:=  (i,i) a:=  (a,a) b:=  (b,b) i<=n a := n b := b+1 a == b a := b+1 b := n return n a:=  (a,a) b:=  (b,b) i:=  (i,i) B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} a:=  (a,a) b:=  (b,b) i:=i+1 B7 {B3} Counters 0000 abni Stacks Before processing B1 Counters 1110 a a0a0 b0b0 n0n0 bni Stacks After processing B1 (but before recursive call) exit B8

9 read(n0) a0 := 1 b0 := 1 n0 <= 1 i0 := 2 i:=  (i,i0) a:=  (a,a0) b:=  (b,b0) i<=n a := n b := b+1 a == b a := b+1 b := n return n a:=  (a,a) b:=  (b,b) i:=  (i,i) B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} a:=  (a,a) b:=  (b,b) i:=i+1 B7 {B3} Counters 1110 a a0a0 b0b0 n0n0 bni Stacks Before processing B2 After processing B2 Counters 1111 a a0a0 b0b0 n0n0 bni Stacks i0i0 exit B8

10 i1i1 read(n0) a0 := 1 b0 := 1 n0 <= 1 i0 := 2 i1:=  (i,i0) a1:=  (a,a0) b1:=  (b,b0) i1<= n0 a := n b := b+1 a == b a := b+1 b := n return n a:=  (a,a) b:=  (b,b) i:=  (i,i) B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} a:=  (a,a) b:=  (b,b) i:=i+1 B7 {B3} After processing B3 Counters 2212 a n0n0 bni Stacks i0i0 Before processing B3 Counters 1111 a a0a0 b0b0 n0n0 bni Stacks i0i0 a1a1 b1b1 b0b0 a0a0 exit B8

11 read(n0) a0 := 1 b0 := 1 n0 <= 1 i0 := 2 i1:=  (i,i0) a1:=  (a,a0) b1:=  (b,b0) i1<= n0 a := n b := b+1 a == b a2 := b1+1 b2 := n0 return n a:=  (a,a) b:=  (b,b) i:=  (i,i) B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} a:=  (a2,a) b:=  (b2,b) i:=i+1 B7 {B3} After processing B4 While processing B4 i1i1 Counters 3312 a n0n0 bni Stacks i0i0 a1a1 b1b1 b0b0 a0a0 i1i1 Counters 3312 a n0n0 bni Stacks i0i0 a1a1 b1b1 b0b0 a0a0 a2a2 b2b2 exit B8

12 read(n0) a0 := 1 b0 := 1 n0 <= 1 i0 := 2 i1:=  (i,i0) a1:=  (a,a0) b1:=  (b,b0) i1<= n0 a3 := n0 b3 := b1+1 a3 == b3 a2 := b1+1 b2 := n0 return n a:=  (a3,a) b:=  (b3,b) i:=  (i1,i) B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} a:=  (a2,a3) b:=  (b2,b3) i:=i+1 B7 {B3} After processing B5 While processing B5 i1i1 Counters 4412 a n0n0 bni Stacks i0i0 a1a1 b1b1 b0b0 a0a0 i1i1 Counters 4412 a n0n0 bni Stacks i0i0 a1a1 b1b1 b0b0 a0a0 a3a3 b3b3 exit B8

13 read(n0) a0 := 1 b0 := 1 n0 <= 1 i0 := 2 i1:=  (i2,i0) a1:=  (a4,a0) b1:=  (b4,b0) i1<= n0 a3 := n0 b3 := b1+1 a3 == b3 a2 := b1+1 b2 := n0 return n a:=  (a3,a) b:=  (b3,b) i:=  (i1,i) B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} a4:=  (a2,a3) b4:=  (b2,b3) i2:=i1+1 B7 {B3} After processing B7 While processing B7 Counters 5513 a n0n0 bni Stacks b0b0 a0a0 i1i1 Counters 5513 a n0n0 bni Stacks i0i0 a1a1 b1b1 b0b0 a0a0 a4a4 b4b4 i2i2 exit B8

14 read(n0) a0 := 1 b0 := 1 n0 <= 1 i0 := 2 i1:=  (i2,i0) a1:=  (a4,a0) b1:=  (b4,b0) i1<= n0 a3 := n0 b3 := b1+1 a3 == b3 a2 := b1+1 b2 := n0 return n0 a:=  (a3,a0) b:=  (b3,b0) i:=  (i1,i) B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} a4:=  (a2,a3) b4:=  (b2,b3) i2:=i1+1 B7 {B3} After processing B6 Before processing B6 Counters 5513 a n0n0 bni Stacks b0b0 a0a0 Counters 5513 a n0n0 bni Stacks b0b0 a0a0 exit B8

15 read(n0) a0 := 1 b0 := 1 n0 <= 1 i0 := 2 i1:=  (i2,i0) a1:=  (a4,a0) b1:=  (b4,b0) i1<= n0 a3 := n0 b3 := b1+1 a3 == b3 a2 := b1+1 b2 := n0 return n0 a5:=  (a3,a0) b5:=  (b3,b0 ) i3:=  (i1,i) B1 B2 B3 B4 B5 B6 B1 B2B6 B3 B4B5 B8 {B7}{B7,B8} {B3,B8} {B8} {} a4:=  (a2,a3) b4:=  (b2,b3) i2:=i1+1 B7 {B3} After processing B8 While processing B8 Counters 6614 abni Stacks Counters 6614 a n0n0 bni Stacks i3i3 b0b0 a0a0 exit B8 a5a5 b5b5