Factored Use-Def Chains and Static Single Assignment Forms

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 - 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.
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.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
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.
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:
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
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.
CS 201 Compiler Construction
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
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.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
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.
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.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
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.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Building SSA Form, I 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at.
Loops Simone Campanoni
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Static Single Assignment
Program Representations
Building SSA Form (A mildly abridged account)
Efficiently Computing SSA
i206: Lecture 13: Recursion, continued Trees
Topic 10: Dataflow Analysis
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
Control Flow Analysis (Chapter 7)
Dominator Tree First BB is the root node, each node
EECS 583 – Class 7 Static Single Assignment Form
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
EECS 583 – Class 8 Finish SSA, Start on Classic Optimization
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
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:

Factored Use-Def Chains and Static Single Assignment Forms Addresses use-def difficulty with optimization: Multiple defs of the same register Which def reaches use? Is RHS expression available? FUD/SSA forms: Each def of a variable is given a unique name All uses reached by that assignment are renamed DU chains become obvious FUD chains and SSA forms are similar r2 = r2 + r3 r6 = r4 - r5 r4 = 4 r6 = 8 r6 = r2 + r6 r7 = r4

Reaching Defs Keeping all reaching defs for each use is not space efficient FUD properties: Each use of a var is reached by a single def Insert pseudo assignments with  nodes where multiple defs meet r23 = r21?+r32? r67 = r45?-r56? r48 = 4 r69 = 8 r612 = r2103+r6117,9 r714 = r413?,8 r6117,9 use defs

Converting to FUD Form r23 = r21?+r32? r67 = r45?-r56? (r4)10?,8 (r6)117,9 r614 = r2123+r61311 r716 = r41510

Converting to SSA Form r2 = r2 + r3 r6 = r4 - r5 r43 = (r41,r42) r63 = (r61,r62) r64 = r22 + r63 r71 = r43

Converting Loops to SSA Form r1 = r1 + 1 … r12 = (r11,r13) r13 = r12 + 1 …

FUD/SSA Pros and Cons Pro Cons Trivial to find what defs reach a use: each use has exactly one def Explicit merging of values at  nodes Simplifies optimizations that need use-def info Cons When transforming code, must either recompute (slow) or incrementally update (tedious)

Phi Nodes A  node merges two reaching definitions for a variable (or register) V A  node for V is required when two non-empty paths XZ and YZ converge at node Z, and nodes X and Y contain assignments to V More precisely: Z is a node with two distinct predecessors Y1 and Y2 p1 : X1* Y1 and p2 : X2* Y2 are two paths in the CFG p1 and p2 have no nodes in common And there are no defs of V along either p1 or p2 (except at X1 and X2)

FUD/SSA Construction Naïve algorithm Problem: too many  nodes Insert  nodes at every join for every variable Solve reaching definitions Rename each use to the def that reaches it Problem: too many  nodes Precision Space Time

FUD Construction Step 1: -term placement using an algorithm that similar to SSA  insertion, but for FUD we also add a slicing edge Step 2: FUD chaining connects each variable use with its unique reaching definition, similar to SSA variable renaming

SSA Construction Step 1: Insert  nodes Step 2: Rename variables

Phi Node Insertion Algorithm Compute dominance frontiers Find global names: Global if name is live on entry to some block For each name build a list of blocks that define it Insert  nodes (-term placement): For each global name N For each basic block B in which N is defined For each basic block D in B’s dominance frontier insert a  node for N in D add N to the list of defining basic blocks

Dominance Frontiers The dominance frontier of a node X is the set of nodes Z = DF(X) such that: If there are two predecessors Y1 and Y2 of Z and X dominates Y1 but not Y2 A node dominates itself A node X dominates a node Y if every path from entry to Y goes through X

Computing Dominance Frontiers Algorithm Compute dominator tree For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D)

Computing Dominance Frontiers (Example Dom. Tree) BB0 BB Dominated by 1 0,1 2 0,1,2 3 0,1,3 4 0,1,3,4 5 0,1,3,5 6 0,1,3,6 7 0,1,7 BB0 BB1 BB1 BB2 BB3 BB2 BB3 BB7 BB4 BB5 BB4 BB5 BB6 Dominator tree BB6 BB7

Computing Dominance Frontiers (Example 1) BB0 BB0 BB DF - 1 2 7 3 4 6 5 BB1 BB1 BB2 BB3 BB7 BB2 BB3 BB4 BB5 BB6 BB4 BB5 BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers BB7

Computing Dominance Frontiers (Example 2) BB0 BB0 BB DF - 1 2 7 3 4 6 5 BB1 D = BB1 BB2 BB3 BB7 Y = BB2 BB3 BB4 BB5 BB6 BB4 BB5 BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers X = BB7

Computing Dominance Frontiers (Example 3) BB0 BB0 BB DF - 1 2 7 3 4 6 5 BB1 D = BB1 BB2 BB3 BB7 BB2 Y = BB3 BB4 BB5 BB6 BB4 BB5 Y = BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers X = BB7

Computing Dominance Frontiers (Example 4) BB0 BB0 BB DF - 1 2 7 3 4 6 5 BB1 BB1 BB2 BB3 BB7 BB2 D = BB3 BB4 BB5 BB6 Y = BB4 BB5 X = BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers BB7

Computing Dominance Frontiers (Example 5) BB0 D = BB0 BB DF - 1 2 7 3 4 6 5 BB1 X = BB1 BB2 BB3 BB7 BB2 BB3 BB4 BB5 BB6 BB4 BB5 BB6 For each join point X in the CFG For each predecessor of X in the CFG go up to the immediate dominator D of X in the dominator tree, adding X to DF(Y) for each Y up to D (excluding X and D) Dominator frontiers Y = BB7

Phi Node Insertion Algorithm Compute dominance frontiers Find global names: Global if name is live on entry to some block For each name build a list of blocks that define it Insert  nodes: For each global name N For each basic block B in which N is defined For each basic block D in B’s dominance frontier insert a  node for N in D add N to the list of defining basic blocks

Phi Node Insertion a is defined in 0, 1, 3: add  in 7 then a is defined in 7: add  in 1 b is defined in 0, 2, 6: add  in 7 then b is defined in 7: add  in 1 c is defined in 0, 1, 2, 5: add  in 6, 7 then c is defined in 6, 7: add  in 1 d is defined in 2, 3, 4: add  in 6, 7 then d is defined in 6, 7: add  in 1 i is defined in 7: add  in 1 BB0 a = b = c = i = BB DF - 1 2 7 3 4 6 5 a = (a,a) b = (b,b) c = (c,c) d = (d,d) i = (i,i) BB1 a = c = BB2 BB3 b = c = d = a = d = BB4 BB5 d = c = a = (a,a) b = (b,b) c = (c,c) d = (d,d) c = (c,c) d = (d,d) BB6 b = For each global name N For each basic block B in which N is defined For each basic block D in B’s dominance frontier insert a  node for N in D add N to the list of defining basic blocks BB7 i =

SSA Construction Step 1: Insert  nodes  Step 2: Rename variables

Rename Variables Algorithm (outline) Use an array of stacks, one per global variable V For each basic block B in a preorder traversal of the dominator tree: Generate unique names for each  node Rewrite each operation in the basic block: for uses of V: get current name from stack for defs of V: create and push new name Fill in  node parameters of successor blocks Recurse on B’s children in the dominator tree On exit from B: pop names generated in B from stacks

Renaming Variables: Initial State BB0 a = b = c = i = Var: a b c d i Ctr: 1 Stk: a0 b0 c0 d0 i0 a = (a,a) b = (b,b) c = (c,c) d = (d,d) i = (i,i) BB1 a = c = BB2 BB3 b = c = d = a = d = BB0 BB4 BB5 d = c = BB1 a = (a,a) b = (b,b) c = (c,c) d = (d,d) c = (c,c) d = (d,d) BB6 BB2 BB3 BB7 b = BB4 BB5 BB6 BB7 i = Preorder traversal of dominator tree

Renaming Variables: After BB0 a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 1 Stk: a0 b0 c0 d0 i0 a = (a0,a) b = (b0,b) c = (c0,c) d = (d0,d) i = (i0,i) BB1 a = c = BB2 BB3 b = c = d = a = d = BB4 BB5 d = c = a = (a,a) b = (b,b) c = (c,c) d = (d,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

Renaming Variables: After BB1 a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 3 2 Stk: a0 a1 a2 b0 b1 c0 c1 c2 d0 d1 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b = c = d = a = d = BB4 BB5 d = c = a = (a,a) b = (b,b) c = (c,c) d = (d,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

Renaming Variables: After BB2 a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 3 4 2 Stk: a0 a1 a2 b0 b1 b2 c0 c1 c2 c3 d0 d1 d2 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a = d = BB4 BB5 d = c = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

Renaming Variables: Backtrack (Before BB3) a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 3 4 2 Stk: a0 a1 a2 b0 b1 c0 c1 c2 d0 d1 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a = d = BB4 BB5 d = c = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

Renaming Variables: After BB3 a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 4 3 2 Stk: a0 a1 a2 a3 b0 b1 c0 c1 c2 d0 d1 d3 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d = c = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c,c) d = (d,d) BB6 b = BB7 i =

Renaming Variables: After BB4 a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 4 3 5 2 Stk: a0 a1 a2 a3 b0 b1 c0 c1 c2 d0 d1 d3 d4 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d4 = c = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c2,c) d = (d4,d) BB6 b = BB7 i =

Renaming Variables: Backtrack to BB3 & After BB5 a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 4 3 5 2 Stk: a0 a1 a2 a3 b0 b1 c0 c1 c2 c4 d0 d1 d3 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d4 = c4 = a = (a2,a) b = (b2,b) c = (c3,c) d = (d2,d) c = (c2,c4) d = (d4,d3) BB6 b = BB7 i =

Renaming Variables: Backtrack to BB3 & After BB6 a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 4 6 2 Stk: a0 a1 a2 b0 b1 b3 c0 c1 c2 c5 d0 d1 d5 i0 i1 a1 = (a0,a) b1 = (b0,b) c1 = (c0,c) d1 = (d0,d) i1 = (i0,i) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d4 = c4 = a = (a2,a3) b = (b2,b3) c = (c3,c5) d = (d2,d5) c5 = (c2,c4) d5 = (d4,d3) BB6 b3 = BB7 i =

Renaming Variables: Backtrack to BB1 & After BB7 a0 = b0 = c0 = i0 = Var: a b c d i Ctr: 5 7 3 Stk: a0 a1 a2 a4 b0 b1 b4 c0 c1 c2 c6 d0 d1 d6 i0 i1 i2 a1 = (a0,a2) b1 = (b0,b4) c1 = (c0,c6) d1 = (d0,d6) i1 = (i0,i2) BB1 a2 = c2 = BB2 BB3 b2 = c3 = d2 = a3 = d3 = BB4 BB5 d4 = c4 = a4 = (a2,a3) b4 = (b2,b3) c6 = (c3,c5) d6 = (d2,d5) c5 = (c2,c4) d5 = (d4,d3) BB6 b3 = BB7 i2 =

Homework: Convert to SSA Form Tasks: Compute dominator tree Compute dominance frontiers Find global names Insert  nodes Rename variables BB0 a = b = BB1 BB2 BB3 c = b = a = BB4 b = BB5 a = c =