© Seth Copen Goldstein & Todd C. Mowry

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Program Representations. Representing programs Goals.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Example in SSA X := Y op Z in out F X := Y op Z (in) = in [ { X ! Y op Z } X :=  (Y,Z) in 0 out F X :=   (in 0, in 1 ) = (in 0 Å in 1 ) [ { X ! E |
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.
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.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
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.
CS 201 Compiler Construction
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
Improving Code Generation Honors Compilers April 16 th 2002.
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.
CS745: Register Allocation© Seth Copen Goldstein & Todd C. Mowry Register Allocation.
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.
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,
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Program Representations. Representing programs Goals.
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.
Computer Science 313 – Advanced Programming Topics.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Simone Campanoni SSA Simone Campanoni
Simone Campanoni CFA Simone Campanoni
Program Representations
Efficiently Computing SSA
Topic 10: Dataflow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
Code Generation Part III
Building SSA Form COMP 512 Rice University Houston, Texas Fall 2003
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
CSC D70: Compiler Optimization Static Single Assignment (SSA)
CS 201 Compiler Construction
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Static Single Assignment Form (SSA)
Optimizations using SSA
Dominator Tree First BB is the root node, each node
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
EECS 583 – Class 8 Finish SSA, Start on Classic Optimization
SSA-based Optimizations
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
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:

© Seth Copen Goldstein & Todd C. Mowry 2001-3 SSA & Opts 15-745 SSA & CCP & DCE & CDG SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Review: Minimal SSA Each assignment generates a fresh variable. At each join point insert  functions for all variables with multiple outstanding defs. x1  1 x  1 y1  x1 y2  2 y  x y  2 y3  (y1,y2) z1  y3 + x1 z  y + x SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Review: Dominance Frontier & path-convergence 1 1 2 5 9 2 5 9 11 6 7 10 11 3 6 7 10 3 12 8 12 8 4 4 13 13 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Constant Propagation If “v  c”, replace all uses of v with c If “v  (c,c,c)” replace all uses of v with c W <- list of all defs while !W.isEmpty { Stmt S <- W.removeOne if S has form “v <- (c,…,c)” replace S with V <- c if S has form “v <- c” then delete S foreach stmt U that uses v, replace v with c in U W.add(U) } SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Other stuff we can do? Copy propogation delete “x  (y)” and replace all x with y delete “x  y” and replace all x with y Constant Folding (Also, constant conditions too!) Unreachable Code Remember to delete all edges from unreachable block SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 SSA & Opts Constant Propagation i1  1 j1  1 k1  0 1 i  1 j  1 k  0 1 j2  (j4,j1) k2  (k4,k1) k2 < 100? 2 k < 100? 2 3 3 4 j < 20? return j 4 j2 < 20? return j2 5 5 6 j  i k  k + 1 j  k k  k + 2 6 j3  i1 k3  k2 + 1 j5  k2 k5  k2 + 2 7 j4  (j3,j5) k4  (k3,k5) 7 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 SSA & Opts Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,j1) k2  (k4,k1) k2 < 100? 2 3 4 j2 < 20? return j2 5 6 j3  i1 k3  k2 + 1 j5  k2 k5  k2 + 2 7 j4  (j3,j5) k4  (k3,k5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 SSA & Opts Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,1) k2  (k4,k1) k2 < 100? 2 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2+ 1 j5  k2 k5  k2+2 7 j4  (j3,j5) k4  (k3,k5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 SSA & Opts Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,1) k2  (k4,k1) k2 < 100? 2 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2 + 1 j5  k2 k5  k2 + 2 7 j4  (1,j5) k4  (k3,k5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 SSA & Opts Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,1) k2  (k4,0) k2 < 100? 2 But, so what? 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2 + 1 j5  k2 k5  k2 + 2 7 j4  (1,j5) k4  (k3,k5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Conditional Constant Propagation SSA & Opts Conditional Constant Propagation i1  1 j1  1 k1  0 1 Does block 6 ever execute? Simple CP can’t tell CCP can tell: Assumes blocks don’t execute until proven otherwise Assumes values are constants until proven otherwise j2  (j4,1) k2  (k4,0) k2 < 100? 2 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2 + 1 j5  k2 k5  k2 + 2 7 j4  (1,j5) k4  (k3,k5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Conditional Constant Propagation SSA & Opts Conditional Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,1) k2  (k4,0) k2 < 100? 2 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2 + 1 j5  k2 k5  k2 + 2 7 j4  (1,j5) k4  (k3,k5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Conditional Constant Propagation SSA & Opts Conditional Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,1) k2  (k4,0) k2 < 100? 2 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2 + 1 j5  k2 k5  k2 + 2 7 j4  (1,j5) k4  (k3,k5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Conditional Constant Propagation SSA & Opts Conditional Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,1) k2  (k4,0) k2 < 100? 2 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2 + 1 j5  k2 k5  k2 + 2 1 7 j4  (1,j5) k4  (k3,k5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Conditional Constant Propagation SSA & Opts Conditional Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,1) k2  (k4,0) k2 < 100? 2 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2 + 1 j5  k2 k5  k2 + 2 1 7 j4  (1,j5) k4  (k3,k5) 1 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Conditional Constant Propagation SSA & Opts Conditional Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,1) k2  (k4,0) k2 < 100? TOP 2 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2 + 1 j5  k2 k5  k2 + 2 1 7 j4  (1,j5) k4  (k3,k5) 1 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Conditional Constant Propagation SSA & Opts Conditional Constant Propagation i1  1 j1  1 k1  0 1 j2  (j4,1) k2  (k4,0) k2 < 100? TOP 2 3 4 j2 < 20? return j2 5 6 j3  1 k3  k2 + 1 j5  k2 k5  k2 + 2 1 7 j4  (1,j5) k4  (k3,k5) 1 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 CCP i1  1 j1  1 k1  0 1 j2  (j,j1) k2  (k4,k1) k < 100? k2  (k3,0) k2 < 100? 2 3 4 j2 < 20? return j2 k3  k2+1 return 1 5 6 j3  i1 k3  k2 + 1 j5  k2 k5  k2 + 2 7 j4  (j3,j5) k4  (k3,k5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Dead Code Elimination Since we are using SSA, this is just a list of all variable assignments. W <- list of all defs while !W.isEmpty { Stmt S <- W.removeOne if |S.users| != 0 then continue if S.hasSideEffects() then continue foreach def in S.definers { def.users <- def.users - {S} if |def.users| == 0 then W <- W UNION {def} } SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Example DCE Standard DCE leaves Zombies! B0 i <- 0 j <- 0 B1 i <- i*2 j <- j+1 j < 10? B1 j1  (j0,j2) i1  (i0,i2) i2 <- i1*2 j2 <- j1+1 j2 < 10? B2 return j B2 return j2 Standard DCE leaves Zombies! SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Aggressive Dead Code Elimination Assume a stmt is dead until proven otherwise. init: mark as live all stmts that have side-effects: - I/O - stores into memory - returns - calls a function that MIGHT have side-effects As we mark S live, insert S.defs into W while (|W| > 0) { S <- W.removeOne() if (S is live) continue; mark S live, insert S.defs into W } SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Example DCE B0 i0<-0 j0<-0 B0 i0<-0 j0<-0 B1 j1  (j0,j2) i1  (i0,i2) i2<-i1*2 j2<-j1+1 j2<10? B2 return j2 B1 j1  (j0,j2) i1  (i0,i2) i2<-i1*2 j2<-j1+1 j2<10? B2 return j2 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Example DCE B0 i0<-0 j0<-0 B1 j1  (j0,j2) i1  (i0,i2) i2<-i1*2 j2<-j1+1 j2<10? B2 return j2 B0 i0<-0 j0<-0 B1 j1  (j0,j2) i1  (i0,i2) i2<-i1*2 j2<-j1+1 j2<10? B2 return j2 Problem! SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Fixing DCE If S is live, then If T determines if S can execute, T should be live j2 j? j? Live Live SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Fixing DCE If S is live, then If T determines if S can execute, T should be live j2 j? j? Live Live SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Control Dependence Y is control-dependent on X if X branches to u and v  a path uexit which does not go through Y  paths vexit go through Y IOW, X can determine whether or not Y is executed. X u v Y exit SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Aggressive Dead Code Elimination Assume a stmt is dead until proven otherwise. while (|W| > 0) { S <- W.removeOne() if (S is live) continue; mark S live, insert - forall operands, S.operand.definers into W - S.CD-1 into W } SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Example DCE B0 i0<-0 j0<-0 B1 j1  (j0,j2) i1  (i0,i2) i2<-i1*2 j2<-j1+1 j2<10? B2 return j2 B0 i0<-0 j0<-0 B1 j1  (j0,j2) i1  (i0,i2) i2<-i1*2 j2<-j1+1 j2<10? B2 return j2 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Example DCE B0 i0<-0 j0<-0 B1 j1  (j0,j2) i1  (i0,i2) i2<-i1*2 j2<-j1+1 j2<10? B2 return j2 B0 j0<-0 B1 j1  (j0,j2) j2<-j1+1 j2<10? B2 return j2 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 CCP Example 1 i  1 j  1 k  0 Does block 6 ever execute? Simple CP can’t tell CCP can tell: Assumes blocks don’t execute until proven otherwise Assumes Values are constants until proven otherwise 2 k < 100? 3 4 j < 20? return j 5 6 j  i k  k + 1 j  k k  k + 2 7 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 CCP -> DCE i1  1 j1  1 k1  0 return 1 k2  (k3,0) k2 < 100? k3 < k2+1 return 1 Small problem. SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Finding the CDG Y is control-dependent on X if X branches to u and v  a path uexit which does not go through Y  paths vexit go through Y IOW, X can determine whether or not Y is executed. X u v Y exit SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

Dominance Frontier & path-convergence START X Y v u 1 1 2 5 9 2 5 9 11 6 7 10 11 3 6 7 10 3 12 8 12 8 4 4 13 13 Any ideas? SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Finding the CDG Y is control-dependent on X if X branches to u and v  a path uexit which does not go through Y  paths vexit go through Y IOW, X can determine whether or not Y is executed. X Y u v src X Y u v exit X Y u v exit SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 Finding the CDG Construct CFG Add entry node and exit node Add (entry,exit) Create G’, the reverse CFG Compute D-tree in G’ (post-dominators of G) Compute DFG’(y) for all y  G’ (post-DF of G) Add (x,y)  G to CDG if x  DFG’(y) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 CDG of example exit 2 1 entry exit B0 i0<-0 j0<-0 B1 j1  (j0,j2) i1  (i0,i2) i2<-i1*2 j2<-j1+1 j2<10? 2 entry 1 B2 return j2 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3

© Seth Copen Goldstein & Todd C. Mowry 2001-3 CDG of example exit 2 1 entry exit exit: {} 2: {entry} 1: {1,entry} 0: {entry} entry: {} 2 entry 1 SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001-3