CS 201 Compiler Construction

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

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.
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.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Static Single Assignment (SSA) Form Jaeho Shin :00 ROPAS Weekly Show & Tell.
Lecture 11: Code Optimization CS 540 George Mason University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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.
Some Properties of SSA Mooly Sagiv. Outline Why is it called Static Single Assignment form What does it buy us? How much does it cost us? Open questions.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
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 7 Code Optimizations: Partial Redundancy Elimination.
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 10 SSA-Based Sparse Conditional Constant Propagation.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
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.
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.
Lecture #17, June 5, 2007 Static Single Assignment phi nodes Dominators Dominance Frontiers Dominance Frontiers when inserting phi nodes.
From last class: CSE Want to compute when an expression is available in a var Domain:
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Data Flow Analysis Compiler Design Nov. 8, 2005.
1 CS 201 Compiler Construction Lecture 10 SSA-Based Sparse Conditional Constant Propagation.
1 CS 201 Compiler Construction Data Flow Analysis.
CSE P501 – Compiler Construction
Dominators, control-dependence and SSA form. Organization Dominator relation of CFGs –postdominator relation Dominator tree Computing dominator relation.
1 June 4, June 4, 2016June 4, 2016June 4, 2016 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel: (800)
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.
Detecting Equality of Variables in Programs Bowen Alpern, Mark N. Wegman, F. Kenneth Zadeck Presented by: Abdulrahman Mahmoud.
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.
Computer Science 313 – Advanced Programming Topics.
Examples of SSA based optimizers: Global Value Numbering.
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
Efficiently Computing SSA
Topic 10: Dataflow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
Chapter 6 Intermediate-Code Generation
CS 201 Compiler Construction
CSC D70: Compiler Optimization Static Single Assignment (SSA)
CS 201 Compiler Construction
Static Single Assignment Form (SSA)
Optimizations using SSA
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:

CS 201 Compiler Construction Lecture 9 Static Single Assignment Form

Program Representations Why develop Advanced Program Representations? To develop faster algorithms To develop more powerful algorithms Superior representation for Data Flow Static Single Assignment Form (SSA Form) superior to def-use chains Superior representation for Control Flow Control Dependence Graph superior to control flow graph

SSA-Form A program in SSA-form satisfies the following two properties: A use of a variable is reached by exactly one definition of that variable. The program is augmented with ϕ–nodes that distinguish values of variables transmitted on distinct incoming control flow edges.

Example K11 L11 Repeat K2ϕ(K1,K5) L2ϕ(L1,L6) if (P) then if (Q) then L32 else L43 L5ϕ(L3,L4) K3K2+1 else K4K2+2 K5ϕ(K3,K4) L6ϕ(L2,L5) Until (T) K  1 L  1 Repeat if (P) then if (Q) then L2 else L3 KK+1 else KK+2 Until (T)

SSA-Form Observations: SSA-form has def-use information textually embedded in it. Given a use, we know where the definition comes from. SSA-form is more compact representation of def-use chains. Def-use chains: #defs x #uses – O(n2) SSA-form: 2 x #defs or #uses – O(n)

Constructing SSA-Form Step 1: Introduce functions at certain points in the program -- v  ϕ (v,v,….) where of operands equals number of control predecessors and ith operand corresponds to ith predecessor.

Contd.. Step 2: Each variable v is given several new names v1, v2, …. Such that every name appears exactly once on the left hand side of an assignment.

Step 1: Introducing ϕ-functions Node Z needs a ϕ-function for variable V if Z is the first node common to two non-null paths that originate at two different nodes each containing: an assignment to V; or a ϕ-function for V.

Step 1 Contd.. Definition: X strictly dominates Y ≅ X dominates Y & X != Y. Definition: Immediate dominator of a node is its closest strict dominator. Notation: X = idom(Y). Definition: Dominance Frontier DF(X) = {Y: there exists P εpred(Y) such that X dominates P & X does not strictly dominate Y} ϕ-functions are placed at nodes in DF nodes of nodes with assignments.

Step 1 Contd.. Strict Domination Does Not Strictly Domination Dominate Y ε DF(X)

Step 1 Contd.. Observation: if Y εDF(X) then there may or may not be a direct edge from X to Y.

Step 1 Contd.. Computing Dominance Frontier: for each Y εsucc(X) do if idom(Y) != X then DF(X) = DF(X) U {Y} for each Z εChidren(X) in the dominator tree do for each Y εDF(Z) do Compute bottom-up order According to dominator tree

Step 1 Contd.. Dominator Tree

Step 1 Contd.. Dominance Frontier of a Set of Nodes S DF(S) = DF(X) Iterated Dominance Frontier DF+(S): DF1 = DF(S) DFi+1 = DF(S U DFi) S – set of nodes which assign to variable V DF+(S) – set of nodes including those where ϕ-functions must be placed.

Step 2: Rename the Variables Step 2: For each variable v rename its left hand side occurrences as v1, v2, …. Perform reaching definition analysis to identify names to use in the right hand side occurrences of v.

Static Single Assignment Sample Problems Static Single Assignment

SSA Form

Applications of SSA Form

Global Value Numbering A technique for determining when two computations in a program are equivalent  can be used for redundancy removal. Constant Propagation -- by computing values of two computations they can be shown to be equivalent. Common Subexpression Elimination -- lexically identical expressions can be shown to be equivalent. Value Numbering -- lexically different expressions can be shown to be equivalent without computing their values.

Examples

Examples

Value Numbering Algorithm

Value Numbering Algorithm