Computer Science 313 – Advanced Programming Topics.

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.
Lecture 11: Code Optimization CS 540 George Mason University.
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.
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.
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.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
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.
Program Representations. Representing programs Goals.
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:
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
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)
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
CS 201 Compiler Construction
Loop invariant detection using SSA An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose single.
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.
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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
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?
Computer Science 313 – Advanced Programming Topics.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
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.
CSCI1600: Embedded and Real Time Software Lecture 33: Worst Case Execution Time Steven Reiss, Fall 2015.
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.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
Mergesort example: Merge as we return from recursive calls Merge Divide 1 element 829.
Static Single Assignment
© Seth Copen Goldstein & Todd C. Mowry
Efficiently Computing SSA
Princeton University Spring 2016
Constructing and Using
CSCI1600: Embedded and Real Time Software
Topic 10: Dataflow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
CS 201 Compiler Construction
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Static Single Assignment Form (SSA)
Optimizations using SSA
Data Flow Analysis Compiler Design
EECS 583 – Class 7 Static Single Assignment Form
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
EECS 583 – Class 7 Static Single Assignment Form
Taken largely from University of Delaware Compiler Notes
Building SSA Harry Xu CS 142 (b) 04/22/2018.
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
CSCI1600: Embedded and Real Time Software
Presentation transcript:

Computer Science 313 – Advanced Programming Topics

Knuth's Rules Of Optimization

Problem We Face

Static Single Assignment  Ignore programmer since they are dumb  Go through and rewrite all local variables NOT  But NOT fields (other threads may use them)  Change code so each variable assigned once (def)  Tie value (use) to definition for that line of code  Names unimportant, since only used by compiler  Optimizations easier & need not fix humans

Examples of SSA Form a = 2; b = a + 1; a = 3; b = a + 1; a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (…) { d = 2; } else { c = 3; } b = a + 1; if (…) { d 1 = 2; } else { c 1 = 3; } b 1 = a 1 + 1;

Control Flow Graph  Common technique showing program structure  Visualizes execution paths possible in a method  Also referred to as flow of a program’s control  Vertices are “basic blocks” found in method  Basic block is code that must be executed together  Edges represent transfer of flow between blocks  Normally result from start & end of loops or branches  Goes to block could come next during a method run

Example of a CFG a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (b 2 > 20) { System.out.println(“Woot”); } else { System.err.print(“Doh”); foo(a 2 ); } b 3 = a 2 + b 2 ;

Example of a CFG a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (b 2 > 20) System.err.print(“Woot”); System.err.print(“Doh”); foo(a 2 ); T F b 3 = a 2 + b 2 ;

Example of a CFG + SSA a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (b 2 > 20) System.err.print(“Woot”); System.err.print(“Doh”); foo(a 2 ); T F b 3 = a 2 + b 2 ;

Limits of SSA Form a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (b 2 > 20) b 3 = 21 foo(a 2 ); T F b 5 = a 2 + b ???? ;

Ф Functions Are Fun!  SSA has problems when code branches  What if variable assigned – cannot know defs to use  Ф function merges values along multiple paths  Ф function has one input for each incoming vertex  1 def for all future use of variable now exists  Remember that Ф function does not exist  Simple bookkeeping to allow SSA to work  Compiler also uses to track related values

Examples of SSA Form a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (b 2 > 20) b 3 = 21 foo(a 2 ); T F b 4 = Ф (b 3, b 2 ); b 5 = a 2 + b 4 ;

Dominators  X dominates Y if and only if ALL PATHS X on ALL PATHS to Y  Must find for good time on weekends

Dominators  X dominates Y if and only if ALL PATHS X on ALL PATHS to Y  Must find for good time on weekends

Dominators  X dominates Y if and only if ALL PATHS X on ALL PATHS to Y  Each basic block needs this to convert to SSA

Dominators  X dominates Y if and only if ALL PATHS X on ALL PATHS to Y  Each basic block needs this to convert to SSA

Dominators  X dominates Y if and only if ALL PATHS X on ALL PATHS to Y  Each basic block needs this to convert to SSA  Reflexive & transitive & fun relation defined  dom(Y) defines dominators of Y  To know when to add Ф nodes for Y use dom(Y)  Must be computed

Dominators  X dominates Y if and only if ALL PATHS X on ALL PATHS to Y  Each basic block needs this to convert to SSA  Reflexive & transitive & fun relation defined  dom(Y) defines dominators of Y  To know when to add Ф nodes for Y use dom(Y)  Must be computed (unless you’ve been a bad coder)

Dominator Tree Example START a b c d END START CFG DT

Dominator Tree Example START a b c d END START CFG DT a

Dominator Tree Example START a b c d END START CFG DT a b c

Dominator Tree Example START a b c d END START CFG DT a b c d

Dominator Tree Example START a b c d END START CFG DT a b c d END

Next Step for SSA Form  Dominance frontier for node X in CFG  Defines set such that for each node Y in the d.f.: X != Y AND X dominates predecessor of Y AND X does not dominate Y

Next Step for SSA Form  Dominance frontier for node X in CFG  Defines set such that for each node Y in the d.f.: X != Y AND X dominates predecessor of Y AND X does not dominate Y

DF Computation  Algorithm to compute dominance frontier for (Vertex b : CFG.vertices()) if ( CFG.countInEdges( b ) ≥ 2) for (Vertex p : CFG.hasEdgeTargetting( b )) runner  p while ( runner != dominatorTree. parent (b)) // Add runner to b ’s dominance frontier runner  dominatorTree. parent (b)

DF Example START a bc d END CFG DT START a bc d END for (Vertex b : CFG.vertices()) if ( CFG.countInEdges( b ) ≥ 2) for (Vertex p : CFG.hasEdgeTargetting( b )) runner  p while ( runner != dominatorTree. parent (b)) // Add b to runner ’s dominance frontier runner  dominatorTree. parent (runner)

DF Example START a bc d END CFG DT START a bc d END for (Vertex b : CFG.vertices()) if ( CFG.countInEdges( b ) ≥ 2) for (Vertex p : CFG.hasEdgeTargetting( b )) runner  p while ( runner != dominatorTree. parent (b)) // Add b to runner ’s dominance frontier runner  dominatorTree. parent (runner)

Placing Φ Nodes  If a basic block X has assignment to variable a  May need Φ function for a but where to place it?  Add Φ to all blocks with X in dominance frontier  Repeat algorithm for each assignment & block  No shortcuts here: must compute iteratively  Quick for computer to do & so can spare the whip

Placing Φ Nodes  If a basic block X has assignment to variable a  May need Φ function for a but where to place it?  Add Φ to all blocks with X in dominance frontier  Repeat algorithm for each assignment & block  No shortcuts here: must compute iteratively  Quick for computer to do & so can spare the whip

Why Should You Care?  Do you understand how programs optimized?  Many common beliefs wrong & ridiculous  Avoid looking like poseur & write useless speedups  May confuse compiler and prevent real optimizations

For Next Lecture  Read pages  What do you when must talk about a program?  What do you document (when it matters?)  What do wish others documentation would say?  Good design means what? Why? How do you know?  Ever seen beautiful code? What made it pretty?