Advanced Compiler Design – Assignment 1 SSA Construction & Destruction Michael Fäs (Original Slides by Luca Della Toffola)

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.
Static Single Assignment (SSA) Form Jaeho Shin :00 ROPAS Weekly Show & Tell.
- 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.
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.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
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.
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.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
CSE P501 – Compiler Construction
Introduction to Optimization, II Value Numbering & Larger Scopes Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Static Single Assignment.
Dominators, control-dependence and SSA form. Organization Dominator relation of CFGs –postdominator relation Dominator tree Computing dominator relation.
Advanced Compiler Design An Introduction to the Javali Compiler Framework Zoltán Majó 1.
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,
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.
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.
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.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Static Single Assignment
Building SSA Form (A mildly abridged account)
Efficiently Computing SSA
Topic 10: Dataflow Analysis
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
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
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.
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:

Advanced Compiler Design – Assignment 1 SSA Construction & Destruction Michael Fäs (Original Slides by Luca Della Toffola)

SSA Pipeline 1.Build the control-flow graph of the program 2.Convert the program into SSA form 3.Apply optimizations 4.Convert the program back to non-SSA form 5.Generate code from the control-flow graph

Assignment 1 Tasks 1.Build the control-flow graph of the program 2.Convert the program into SSA form 3.Apply optimizations 4.Convert the program back to non-SSA form 5.Generate code from the control-flow graph (Due date: Mar 18, 2015)

SSA Construction 1.Compute Dominance Frontier Using Dominator Tree 2.Insert ɸ-operations where necessary In join nodes with different incoming assignments Use dominance frontier 3.Add versions to variables (renaming)

SSA Destruction 1.Replace ɸ-operations with copy statements in predecessor blocks

DOMINANCE RELATIONS REVIEW Assignment 1

Dominator A (control flow graph) node y dominates n if y lies in every path from the root n 0 to n. D OM ( n ) = the set of all dominators of n Note: Every node dominates itself The root node dominates all nodes

n D OM ( n ) A{root, A} B{root, B} C{root, A, C} D{root, D} E{root, B, E} F{root, A, C, F} G{root, D, G} H{root, H} A D B EC GF H root

SD OM ( n ) = the set of strict dominators of n Note: SD OM ( n ) = D OM ( n ) \ { n } Strict Dominator A node y strictly dominates n if y dominates n and n ≠ y.

A D B EC GF H root n SD OM ( n ) A{root} B C{root, A} D{root} E{root, B} F{root, A, C} G{root, D} H{root}

ID OM ( n ) = the immediate dominator of n Note: Every node (except the root) has exactly one immediate dominator Immediate Dominator Node y immediately dominates n if y is the closest strict dominator of n on any path from n 0 to n.

A D B EC GF H root n ID OM ( n ) A{root} B C{A} D{root} E{B} F{C} G{D} H{root}

Dominator Tree Contains all nodes of the CFG with edges that reflect the dominance relation Each node n is a child of its ID OM ( n ) Bottom-up traversal results in D OM ( n )

A D B EC GF H CFG A DB ECG F H Dominator Tree

DOMINATOR TREE CONSTRUCTION Assignment 1 – SSA Construction

Iterative Dominator Algorithm Idea: Solve the following data-flow problem: For n  n 0, initialize D OM ( n ) with all nodes Update D OM ( n ) according to equations Walk over the CFG, repeat until no changes.

{A,C,D} {A,E} {A,B} {A,C} A C B D E D OM ( root ) = { root } for n in nodes \ { root }: D OM ( n ) = nodes while changed : for n in nodes : pre = intersect(D OM ( preds ( n ))) new = union( pre, n ) D OM ( n ) = new Dominator Algorithm: Example {A} {A,B,C,D,E}

Computing Dominators Shown algorithm – Simple but not very efficient – How to get ID OM ( n ) from D OM ( n ) ? Efficient algorithm described in A Simple, Fast Dominance Algorithm by Cooper et al. – Same idea, still very simple – Not required for getting full marks – Also covers shown algorithm

DOMINANCE FRONTIER CONSTRUCTION Assignment 1 – SSA Construction

Dominance Frontier DF( n ) = all nodes y such that n dominates a predecessor of y but does not strictly dominate y. Intuition: The Dominance Frontier of n is where n ’s dominance “ends”. in the CFG

Dominance Frontier: Intuition H A B F C D J I G E

Dominance Frontier: Example A CE D B DF(A) = {} DF(B) = {D} DF(D) = {} DF(E) = {C} DF(C) = {C, D} n dominates a predecessor of y and n not strictly dominates y

Computing the DF Idea: – Given node n, do not compute DF( n ) directly – Instead, find nodes y for which n is in DF( y ) Simple algorithm based on two observations

Computing the DF: Observation 1 Predecessors of a node n have n in their DF unless they dominate n. A C D B I II

Computing the DF: Observation 2 Dominators of predecessors of a node n have n in their DF unless they dominate n. A E F C B D … … …

for n in nodes : for p in preds ( n ): r = p while r != I DOM ( n ): DF( r ) += n r = I DOM ( r ) A C E D B A D C E B Dominator Tree CFG n = D DF(B) = {D} DF(C) = {D} n = C DF(E) = {C} DF(C) = {C,D} DF(A)= {} DF(B)= {D} DF(C)= {C,D} DF(E)= {C} DF(D)= {} Dominance Frontier

ɸ-FUNCTION INSERTION & VARIABLE VERSIONING Assignment 1 – SSA Construction

ɸ-Function Insertion ɸ-functions need to be inserted in all join nodes with different incoming assignments ɸ-operations are assignments too! Assignments require a ɸ-function to be inserted in the DF nodes of the block containing the assignment BB4 x = ɸ(x,x) write(x); BB1 x = 0 BB2 x = 1 BB3 write(x)

DF and ɸ-Functions: Example H A B F C D J I G E x = 1 x = 4 x = ɸ(x,x) write(x) x = ɸ(x,x,x) write(x)

BB4 x = ɸ(x,x) write(x); ɸ-Function Insertion: Example BB1 x = 0 COND: y==0 BB2 x = 1 BB3 write(x) truefalse x = 0; if(y == 0) { x = 1; } else { write(x); } write(x);

Variable Versioning Need to add versions to all variables – S.t. each version is assigned only once (statically) Make sure that always the “newest” version of a variable is used – Traverse the CFG, keep track of current (newest) and highest variable versions Pro tip: Dominator tree... – Replace all uses of unversioned variables Also in ɸ-functions!

Variable Versioning: Example H A B F C D J I G E x = 1 x = 4 x = ɸ(x,x ) write(x ) x = ɸ(x,x,x ) write(x )

Variable Versioning: Example BB6 x = ɸ(x,x ) write(x ); BB1 x = 0 BB2 x = 1 BB3 write(x ) BB5 x = x + 1 BB4 x = ɸ(x,x )

SSA DESTRUCTION Assignment 1

SSA Destruction Replace ɸ-operations with copy statements in predecessors

Removing ɸ-Functions: Example 1 BB1 x 1 = 0 BB2 x 2 = 1 x 3 = x 2 BB3 write(x 1 ) x 3 = x 1 BB4 x 3 = ɸ(x 2,x 1 ) write(x 3 )

Removing ɸ-Functions: Example 2 BB1 x 1 = 0 x 3 = x 1 BB2 x 2 = 1 x 3 = x 2 BB3 x 3 = ɸ(x 2,x 1 ) write(x 3 ) Process may insert redundant assignments: x 3 = x 1... x 3 = x 2 Solution: Insert empty blocks where necessary

Removing ɸ-Functions: Example 2 BB1 x 1 = 0 BB2 x 2 = 1 x 3 = x 2 BB3 x 3 = ɸ(x 2,x 1 ) write(x 3 ) Process may insert redundant assignments: x 3 = x 1... x 3 = x 2 Solution: Insert empty blocks where necessary – Already done in CFG construction phase in A1 fragment BB4 x 3 = x 1

PRACTICAL TIPS (FRAMEWORK) Assignment 1

Framework: Dominator Tree Computation of I DOM ( n ) and DF( n ) in cd.cfg.Dominator class Dominator tree edges directly in BasicBlock : dominatorTreeParent: BB dominatorTreeChildren: List dominanceFrontier: Set cd.ir.BasicBlock I DOM ( n )

Framework: Variable Versions VariableSymbol has new field: version New constructor to create a new version: VariableSymbol(VariableSymbol v0sym, int version)

Framework: ɸ-Operations ɸ-operations represented by class cd.ir.Phi write(x) x 3 = ɸ(x 1,x 2 ) write(x 3 ) v0sym: VariableSymbol lhs: VariableSymbol rhs: List cd.ir.Phi

Framework: ɸ-Operations BasicBlock contains all attached ɸ-ops Map key is original variable (version 0) Add the new symbols to the method locals phis: Map cd.ir.BasicBlock

? Questions