Static Single Assignment CS 540. Spring 2010 2 Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Static Single-Assignment ? ? Introduction: Over last few years [1991] SSA has been Stablished as… Intermediate program representation.
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.
Lecture 11: Code Optimization CS 540 George Mason University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
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.
Loop invariant code removal CS 480. Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k]
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.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
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:
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
CS 536 Spring Global Optimizations Lecture 23.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Lecture #17, June 5, 2007 Static Single Assignment phi nodes Dominators Dominance Frontiers Dominance Frontiers when inserting phi nodes.
CS 201 Compiler Construction
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic H: SSA for Predicated Code José Nelson Amaral
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
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...
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.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Precision Going back to constant prop, in what cases would we lose precision?
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
Extracting Performance Functions. Basic Operations n The number of Basic Operations performed must be proportional to the run time n Counting techniques.
sequence of execution of high-level statements
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Detecting Equality of Variables in Programs Bowen Alpern, Mark N. Wegman, F. Kenneth Zadeck Presented by: Abdulrahman Mahmoud.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Efficiently Computing SSA
Princeton University Spring 2016
Topic 10: Dataflow Analysis
CS 201 Compiler Construction
CS 201 Compiler Construction
Static Single Assignment Form (SSA)
Optimizations using SSA
Computer Science Core Concepts
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.
Iteration – While Loops
Presentation transcript:

Static Single Assignment CS 540

Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation in how easy it is to use, and how easy it is to generate. Static Single Assignment (SSA)

CS540 Spring Consider the following (1)k = 2 (2) if k > 5 then (3) k = k + 1 (4) m = k * 2 (5) else (6) m = k / 2 (7) endif (8) k = k + m (1)k = 2 (2) if k (1) > 5 then (3) k = k (1) + 1 (4) m = k (3) * 2 (5) else (6) m = k (1) / 2 (7) endif (8) k = k (1,3) + m (4,6) The uses in each statement have been marked with the statement number of all definitions that reach.

CS540 Spring Static Single Assignment Idea: Each definition will be uniquely numbered. There will be a single reaching definition for each point. Algorithms for static single assignment are space efficient and take control flow into account.

CS540 Spring SSA numbering (1)k = 2 (2) if k > 5 then (3) k = k + 1 (4) m = k * 2 (5) else (6) m = k / 2 (7) endif (8) k = k + m (1)k1 = 2 (2) if k1 > 5 then (3) k2 = k1 + 1 (4) m1 = k2 * 2 (5) else (6) m2 = k1 / 2 (7) endif (8) k3 = k??+ m?? Problem: Because of multiple reaching definitions, we can’t give each use a unique number without analysis.

CS540 Spring  Functions (1)k = 2 (2) if k > 5 then (3) k = k + 1 (4) m = k * 2 (5) else (6) m = k / 2 (7) Endif (8) k = k + m (1)k1 = 2 (2) if k1 > 5 then (3) k2 = k1 + 1 (4) m1 = k2 * 2 (5) else (6) m2 = k1 / 2 (7) Endif k3 =  (k1,k2) m3 =  (m1,m2) (8) k4 = k3+ m3  functions - merge definitions, factoring in control flow

CS540 Spring SSA for Structured Code Associate with each variable x, a current counter xc. Assignment statement: x := y op z becomes x xc++ := y yc op z zc

CS540 Spring SSA for Structured Code Loops: Repeat S until E for all variables M with definition k in loop body if M has a definition j above the loop then generate M Mc++ := f (M k,M j ); at loop start s = 1s 1 = 1repeat … … s 3 =  (s 1,s 2 ) s = s + 1 s 2 = s until s > 5until s 2 > 5

CS540 Spring Computing SSA for Structured Code Conditionals: –if E then S1 else S2 for all variables M with definition in either S1 or S2 Case 1: M has definition j in S1 and definition k in S2 Generate M Mc++ :=  (M k, M j ); after the conditional if … then a := b a j := b n else a := c a k := c m a l =  (a j,a k )

CS540 Spring Computing SSA for Structured Code Conditionals: –if E then S1 else S2 for all variables M with definition in either S1 or S2 Case 2: M has definition k in S1 or in S2, definition j above the conditional Generate M Mc++ :=  (M k, M j ); after the conditional a := ca j := c m if … then a := b a k := b n else …else … a l =  (a j,a k )

CS540 Spring Computing SSA for Structured Code Conditionals: –if E then S1 for all variables M with definition k in S1 and definition j that reaches the conditional, generate M Mc++ :=  (M k, M j ); after the conditional a := ca j := c m if … then a := b a k := b n a l =  (a j,a k )

CS540 Spring i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T

CS540 Spring i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i1 = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i2 = i + 6 until T Number existing defns

CS540 Spring i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i1 = j = k = l = 1 repeat i3 =  () if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i2 = i + 6 until T Add  definitions where needed

CS540 Spring i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i1 = j = k = l = 1 repeat i3 =  (i1,i2) if (p) then begin j = i3 if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i3,j,k,l) repeat if R then l = l + 4 until S i2 = i3 + 6 until T Fill in the use numbers

CS540 Spring i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i = j1 = k = l = 1 repeat j2 =  (j1,j4) if (p) then begin j3 = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 j4 =  (j2,j3) print (i,j4,k,l) repeat if R then l = l + 4 until S i = i + 6 until T

CS540 Spring i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i = j = k1 = l = 1 repeat k2 =  (k1,k5) if (p) then begin j = i if Q then l = 2 else l = 3 k3 = k2 + 1 end else k4 = k2 + 2 k5 =  (k3,k4) print (i,j,k5,l) repeat if R then l = l + 4 until S i = i + 6 until T

CS540 Spring i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i = j = k = l1 = 1 repeat l2 =  (l1,l9) if (p) then begin j = i if Q then l3 = 2 else l4 = 3 l5 =  (l3,l4) k = k + 1 end else k = k + 2 l6 =  (l2,l5) print (i,j,k,l6) repeat l7 =  (l6,l9) if R then l8 = l7 + 4 l9 =  (l7,l8) until S i = i + 6 until T

CS540 Spring i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i1 = j1 = k1 = l1 = 1 repeat i3 =  (i1,i2) j2 =  (j1,j4) k2 =  (k5,k1) l2 =  (l9,l1) if (p) then begin j3 = i2 if Q then l3 = 2 else l4 = 3 l5 =  (l3,l4) k3 = k2 + 1 end else k4 = k2 + 2 j4 =  (j3,j2) k5 =  (k3,k4) l6 =  (l2,l5) print (i3,j4,k5,l6) repeat l7 =  (l9,l6) if R then l8 = l7 + 4 l9 =  (l7,l8) until S i2 = i3 + 6 until T

Using SSA for Constant Propagation For statements x i := C, for some constant C, replace all x i with C and remove the statement For x i :=  (c,c,...,c), for some constant c, replace statement with x i := c Can extend to evaluate conditional branches Iterate Locates AND “Performs” the replacement

Example: SSA a := 3 d := 2 f := a + d g := 5 a := g – d f < = g f := g + 1 g < a d := 2 T F T F a1 := 3 d1 := 2 d3 =  (d1,d2) a3 =  (a1,a2) f1 := a3 + d3 g1 := 5 a2 := g1 – d3 f1 <= g1 f2 := g1 + 1 g1 < a2 f3 :=  (f1,f2) d2 := 2 T F T F

Example: SSA a1 := 3 d1 := 2 d3 =  (d1,d2) a3 =  (a1,a2) f1 := a3 + d3 g1 := 5 a2 := g1 – d3 f1 <= g1 f2 := g1 + 1 g1 < a2 f3 :=  (f1,f2) d2 := 2 T F T F a1 := 3 d1 := 2 d3 =  (2,2) a3 =  (3,a2) f1 := a3 + d3 g1 := 5 a2 := 5 – d3 f1 <= 5 f2 := < a2 f3 :=  (f1,f2) d2 := 2 T F T F

Example: SSA a1 := 3 d1 := 2 d3 =  (2,2) a3 =  (3,a2) f1 := a3 + d3 g1 := 5 a2 := 5 – d3 f1 <= 5 f2 := < a2 f3 :=  (f1,f2) d2 := 2 T F T F a1 := 3 d1 := 2 d3 =  a3 =  (3,a2) f1 := a3 + 2 g1 := 5 a2 := 5 – 2 f1 <= 5 f2 := 6 5 < a2 f3 :=  (f1,6) d2 := 2 T F T F

Example: SSA a1 := 3 d1 := 2 d3 =  a3 =  (3,a2) f1 := a3 + 2 g1 := 5 a2 := 5 – 2 f1 <= 5 f2 := 6 5 < a2 f3 :=  (f1,6) d2 := 2 T F T F a1 := 3 d1 := 2 d3 =  a3 =  (3,3) f1 := a3 + 2 g1 := 5 a2 := 3 f1 <= 5 f2 := 6 5 < 3 f3 :=  (f1,6) d2 := 2 T F T F

Example: SSA a1 := 3 d1 := 2 d3 =  a3 =  (3,3) f1 := a3 + 2 g1 := 5 a2 := 3 f1 <= 5 f2 := 6 5 < 3 f3 :=  (f1,6) d2 := 2 T F T F a1 := 3 d1 := 2 d3 =  a3 =  f1 := g1 := 5 a2 := 3 f1 <= 5 f2 := 6 f3 :=  (f1,6) d2 := 2 T F

Example: SSA a1 := 3 d1 := 2 d3 =  a3 =  f1 := g1 := 5 a2 := 3 f1 <= 5 f2 := 6 f3 :=  (f2) d2 := 2 T F a1 := 3 d1 := 2 d3 =  a3 =  f1 := g1 := 5 a2 := 3 true f2 := 6 f3 :=  (6) d2 := 2

Example: SSA a1 := 3 d1 := 2 d3 =  a3 =  f1 := 5 g1 := 5 a2 := 3 true f2 := 6 f3 :=  (6) d2 := 2 a1 := 3 d1 := 2 d3 =  a3 =  f1 := g1 := 5 a2 := 3 f2 := 6 d2 := 2

CS540 Spring SSA Deconstruction At some point, we need executable code Can’t implement Ø operations Need to fix up the flow of values Basic idea Insert copies Ø-function pred’s Simple algorithm –Works in most cases Adds lots of copies –Most of them coalesce away X 17  Ø(x 10,x 11 )...  x  x 17 X 17  x 10 X 17  x 11 *

CS540 Spring           Exit k0 =  k1 = k0 k4 =  (k2,k3) k1 =  (k0,k4) k3 =  k1 + 2 k4 = k3 k2 =  k1 + 1 k4 = k2 i = j = k0 = l = 1 k1 = k0 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k2 = k2 + 1 k4 = k2 end else k3 = k1 + 2 k4 = k3 print (i,j,k4,l) repeat if R then l = l + 4 until S i = i + 6 k1 = k4 until T k1 =  k4

CS 540 Spring 2010 GMU30 Final Exam 75%-80% (ish) on material since the midterm –Syntax directed translation (a little of this on midterm) –Symbol table & types –Intermediate code –Runtime Environments –Code Generation –Code Optimization Remaining – HL concepts from the first part of the semester

CS 540 Spring 2010 GMU31 Final Exam: Syntax directed translation & Types SDT: –Some on midterm already –Got lots of practice (program #3, #4) –Understanding/Creating Symbol Tables & Types –Types Terminology –Scope –Table implementation

CS 540 Spring 2010 GMU32 Final Exam: Intermediate Code & RT Environments Intermediate Code –Expressions, Control constructs –Should be able to write/understand basic code –Don’t memorize spim – will put info on exam RT Environments –Control flow –Data flow –Variable addressing –Parameter passing

CS 540 Spring 2010 GMU33 Final Exam: Code Generation & Code Optimization Code Generation –Instr. selection/Instruction scheduling: only what they are trying to accomplish –Register allocation: understand how to use liveness to allocation, graph coloring to assign –Review the example on the slides – could get a question like that Code Optimization –Gave lots of examples of optimizations that are useful –Dataflow analysis basics – I won’t make you use the equations (would take too long) but you should have a general idea what is being done.