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.

Slides:



Advertisements
Similar presentations
Continuing Abstract Interpretation We have seen: 1.How to compile abstract syntax trees into control-flow graphs 2.Lattices, as structures that describe.
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.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
SSA.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
3-Valued Logic Analyzer (TVP) Tal Lev-Ami and Mooly Sagiv.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
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.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Early Global Program Optimizations Chapter Mooly Sagiv.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 CS 201 Compiler Construction Lecture 10 SSA-Based Sparse Conditional Constant Propagation.
Program analysis Mooly Sagiv html://
CS 536 Spring Global Optimizations Lecture 23.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Program analysis Mooly Sagiv html://
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
CS 201 Compiler Construction
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
Early Program Optimizations Chapter 12 Mooly Sagiv.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Overview of program analysis Mooly Sagiv html://
Improving Code Generation Honors Compilers April 16 th 2002.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
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.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 CS 201 Compiler Construction Lecture 10 SSA-Based Sparse Conditional Constant Propagation.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Procedure Optimizations and Interprocedural Analysis Chapter 15, 19 Mooly Sagiv.
Precision Going back to constant prop, in what cases would we lose precision?
CSE P501 – Compiler Construction
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
PHP Constructs Advance Database Management Systems Lab no.3.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Final Code Generation and Code Optimization.
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.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Control Flow Testing Handouts
Outline of the Chapter Basic Idea Outline of Control Flow Testing
Interprocedural Analysis Chapter 19
CS1100 Computational Engineering
CS 201 Compiler Construction
CS 201 Compiler Construction
Optimizations using SSA
Computer Science Core Concepts
Final Code Generation and Code Optimization
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
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:

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

Static Single Assignment Every variable is assigned once But in loops a variable will be assigned many times

An Acyclic Example x  x + 1 x  x + 2 x > 0 x  x + 3 x  x + 4 x > 0 x  x + 5 x  x + 6

A Cyclic Example i = 1; j = 1; k = 0 while (k < 0) { if (j < 0) { j = i; k = k + 1; } else { j = k ; k = k + 2 } return j

What does it buy us? Compact representation of DU chains Convert “imperative” into “functional” style Extended basic blocks Allows flow-insensitive analysis

How much does it cost Code size –Worst case the extra number of assignments is quadratic –For programs with while-do and if-then-else linear –In practice linear Construction of SSA

Open Questions Dealing with pointers When is it useful? Is it really needed? –Can we choose an appropriate data structure in the iterative data flow algorithm instead

Conditional Constant Propagation Conditions with constant values can be interpreted to improve precision A more precise solution is obtained “optimistically”

char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3 );} main() { printf(“%s\n”, “red”);} “red”

char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3);}

Iterative Data-Flow Algorithm Input: a flow graph G=(N,E,r) An init value Init A montonic function F B for every B in N Output:For every N in(N) Initializatio: in(Entry) := Init; for each node B in N-{Entry} do in(B) :=  WL := N - {Entry} Iteration: while WL != {} do Select and remove an B from WL out := F B (in(B)) For all B’ in succ(B) such that in(B’) != in(B’)  out do in(B’):= in(B’)  out WL := WL  {B’}

Iterative Data-Flow Algorithm Input: a flow graph G=(N,E,r) An init value Init A montonic function F B for every B in N Output:For every N in(N) Initializatio: in(Entry) := Init; for each node B in N-{Entry} do in(B) :=  WL := {Entry} Iteration: while WL != {} do Select and remove an B from WL out := F B (in(B)) For all B’ in succ(B) such that in(B’) != in(B’)  out do in(B’):= in(B’)  out WL := WL  {B’}

char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3);}

Conditional Constant Propagation initialize the worklist to the entry node mark all edges as not executable repeat until the worklist is empty: –select and remove a node from the worklist –if it is an assignment then mark the successor edge as executable –if it is a test then symbolically evaluate the test and mark the enabled successor edges as executable if test evaluates to true or  mark true edge executable if test evaluates to false or  mark false edge executable –update the value of all the variables at the entry and exit of this node –if there are changes then add all successors reachable from the node with edges marked executable to the worklist

Sparse Conditional Constant bring the program in SSA form initialize the analysis information: –all variables are mapped to  –all flow edges are marked as not executable initialize the two worklists –Flow-Worklist contains all edges of the flow graph with the entry node as source –SSA-Worklist is empty

repeat until both worklists are empty: –select and remove an edge from one of the worklists –if it is a flow edge then if the edge is not marked executable then –mark it executable –if the target of the edge is a  -node then call visit-  –if it is the first time the node is visited (only one incoming flow edge is marked executable) and it is a normal node then call visit-instr –if it is an SSA edge then if the target of the edge is a  -node then call visit-  if it is a normal node and at least one of the flow edges entering the node are marked executable then call visit-instr

visit-  : (the node is a  -node) –the assigned variable is given a value that is the join the values of the arguments with incoming edges marked executable visit-instr: (the node is a normal node) –determine the value of the expression of the node and update the variable in case of an assignment –if there are changes then if the node is an assignment then add all SSA edges with source at the target of the current edge to the SSA-worklist if the node is a test then add all relevant flow edges to the Flow- worklist and mark them executable –if test evaluates to true or  add true edge –if test evaluates to false or  : add false edge