15 745 SSA CCP DCE CDG SSA Opts

  • Slides: 37
Download presentation
15 -745 SSA & CCP & DCE & CDG SSA & Opts © Seth

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

Review: Minimal SSA • Each assignment generates a fresh variable. • At each join

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

Review: Dominance Frontier & path-convergence 1 2 3 4 1 5 6 9 7

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

Constant Propagation • If “v c”, replace all uses of v with c •

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. is. Empty { Stmt S <- W. remove. One 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 4

Other stuff we can do? • Copy propogation – delete “x (y)” and replace

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 5

Constant Propagation 1 1 i 1 j 1 k 0 2 k < 100?

Constant Propagation 1 1 i 1 j 1 k 0 2 k < 100? 3 j < 20? 5 j i k k + 1 2 4 return j 3 6 j k 5 k k + 2 j 2 (j 4, j 1) k 2 (k 4, k 1) k 2 < 100? j 2 < 20? 4 return j 2 6 j 5 k 2 j 3 i 1 k 3 k 2 + 1 k 5 k 2 + 2 7 SSA & Opts i 1 1 j 1 1 k 1 0 7 j 4 (j 3, j 5) k 4 (k 3, k 5) © Seth Copen Goldstein & Todd C. Mowry 2001 -3 6

Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0

Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, j 1) k 2 (k 4, k 1) k 2 < 100? j 2 < 20? 5 j 3 i 1 k 3 k 2 + 1 SSA & Opts 4 return j 2 6 j 5 k 2 k 5 k 2 + 2 7 j 4 (j 3, j 5) k 4 (k 3, k 5) © Seth Copen Goldstein & Todd C. Mowry 2001 -3 7

Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0

Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, 1) k 2 (k 4, k 1) k 2 < 100? j 2 < 20? 5 j 3 1 k 3 k 2+ 1 SSA & Opts 4 return j 2 6 j 5 k 2 k 5 k 2+2 7 j 4 (j 3, j 5) k 4 (k 3, k 5) © Seth Copen Goldstein & Todd C. Mowry 2001 -3 8

Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0

Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, 1) k 2 (k 4, k 1) k 2 < 100? j 2 < 20? 4 return j 2 5 6 j 5 k 2 j 3 1 k 3 k 2 + 1 k 5 k 2 + 2 7 j 4 (1, j 5) k 4 (k 3, k 5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 9

Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0

Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, 1) k 2 (k 4, 0) k 2 < 100? j 2 < 20? But, so what? 4 return j 2 5 6 j 5 k 2 j 3 1 k 3 k 2 + 1 k 5 k 2 + 2 7 j 4 (1, j 5) k 4 (k 3, k 5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 10

Conditional Constant Propagation i 1 1 j 1 1 k 1 0 1 3

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

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, 1) k 2 (k 4, 0) k 2 < 100? j 2 < 20? 4 return j 2 5 6 j 5 k 2 j 3 1 k 3 k 2 + 1 k 5 k 2 + 2 7 j 4 (1, j 5) k 4 (k 3, k 5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 13

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, 1) k 2 (k 4, 0) k 2 < 100? j 2 < 20? 4 return j 2 5 6 j 5 k 2 j 3 1 k 3 k 2 + 1 k 5 k 2 + 2 7 j 4 (1, j 5) k 4 (k 3, k 5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 14

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, 1) k 2 (k 4, 0) k 2 < 100? j 2 < 20? 4 return j 2 5 6 j 5 k 2 j 3 1 k 3 1 k 2 + 1 k 5 k 2 + 2 7 j 4 (1, j 5) k 4 (k 3, k 5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 15

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, 1) k 2 (k 4, 0) k 2 < 100? j 2 < 20? 4 return j 2 5 6 j 5 k 2 j 3 1 k 3 1 k 2 + 1 k 5 k 2 + 2 7 j 4 (1, j 5) k 4 1 (k 3, k 5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 16

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, 1) k 2 TOP (k 4, 0) k 2 < 100? j 2 < 20? 4 return j 2 5 6 j 5 k 2 j 3 1 k 3 1 k 2 + 1 k 5 k 2 + 2 7 j 4 (1, j 5) k 4 1 (k 3, k 5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 17

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1

Conditional Constant Propagation 1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j 4, 1) k 2 TOP (k 4, 0) k 2 < 100? j 2 < 20? 4 return j 2 5 6 j 5 k 2 j 3 1 k 3 1 k 2 + 1 k 5 k 2 + 2 7 j 4 (1, j 5) k 4 1 (k 3, k 5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 18

1 2 3 i 1 1 j 1 1 k 1 0 j 2

1 2 3 i 1 1 j 1 1 k 1 0 j 2 (j, j 1) k 2 (k 4, k 1) k < 100? j 2 < 20? 5 CCP j 3 i 1 k 3 k 2 + 1 k 2 (k 3, 0) k 2 < 100? 4 return j 2 k 3 k 2+1 return 1 6 j 5 k 2 k 5 k 2 + 2 7 j 4 (j 3, j 5) k 4 (k 3, k 5) SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 19

Dead Code Elimination W <- list of all defs while !W. is. Empty {

Dead Code Elimination W <- list of all defs while !W. is. Empty { Since we are using SSA, this is just a list of all variable assignments. Stmt S <- W. remove. One if |S. users| != 0 then continue if S. has. Side. Effects() 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 20

Example DCE B 0 i <- 0 B 0 j <- 0 B 1

Example DCE B 0 i <- 0 B 0 j <- 0 B 1 i 0 <- 0 j 0 <- 0 i <- i*2 B 1 j <- j+1 j 1 (j 0, j 2) i 1 (i 0, i 2) i 2 <- i 1*2 j < 10? j 2 <- j 1+1 j 2 < 10? B 2 return j 2 Standard DCE leaves Zombies! SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 21

Aggressive Dead Code Elimination Assume a stmt is dead until proven otherwise. init: mark

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. remove. One() if (S is live) continue; mark S live, insert S. defs into W } SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 22

Example DCE B 0 i 0<-0 B 0 j 0<-0 B 1 B 2

Example DCE B 0 i 0<-0 B 0 j 0<-0 B 1 B 2 SSA & Opts j 1 (j 0, j 2) i 1 (i 0, i 2) i 0<-0 j 0<-0 B 1 j 1 (j 0, j 2) i 1 (i 0, i 2) i 2<-i 1*2 j 2<-j 1+1 j 2<10? return j 2 B 2 return j 2 © Seth Copen Goldstein & Todd C. Mowry 2001 -3 23

Example DCE B 0 i 0<-0 B 0 j 0<-0 B 1 B 2

Example DCE B 0 i 0<-0 B 0 j 0<-0 B 1 B 2 i 0<-0 j 1 (j 0, j 2) i 1 (i 0, i 2) B 1 j 1 (j 0, j 2) i 1 (i 0, i 2) i 2<-i 1*2 j 2<-j 1+1 j 2<10? return j 2 B 2 return j 2 Problem! SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 24

Fixing DCE If S is live, then If T determines if S can execute,

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

Fixing DCE If S is live, then If T determines if S can execute,

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

Control Dependence Y is control-dependent on X if • X branches to u and

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 27

Aggressive Dead Code Elimination Assume a stmt is dead until proven otherwise. while (|W|

Aggressive Dead Code Elimination Assume a stmt is dead until proven otherwise. while (|W| > 0) { S <- W. remove. One() 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 28

Example DCE B 0 i 0<-0 B 0 j 0<-0 B 1 B 2

Example DCE B 0 i 0<-0 B 0 j 0<-0 B 1 B 2 SSA & Opts j 1 (j 0, j 2) i 1 (i 0, i 2) i 0<-0 j 0<-0 B 1 j 1 (j 0, j 2) i 1 (i 0, i 2) i 2<-i 1*2 j 2<-j 1+1 j 2<10? return j 2 B 2 return j 2 © Seth Copen Goldstein & Todd C. Mowry 2001 -3 29

Example DCE B 0 i 0<-0 B 0 j 0<-0 B 1 j 1

Example DCE B 0 i 0<-0 B 0 j 0<-0 B 1 j 1 (j 0, j 2) i 1 (i 0, i 2) j 0<-0 B 1 j 1 (j 0, j 2) i 2<-i 1*2 B 2 SSA & Opts j 2<-j 1+1 j 2<10? return j 2 B 2 return j 2 © Seth Copen Goldstein & Todd C. Mowry 2001 -3 30

CCP Example 1 3 j 5 j k i 1 j 1 k 0

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

CCP -> DCE i 1 1 j 1 1 k 1 0 return 1

CCP -> DCE i 1 1 j 1 1 k 1 0 return 1 k 2 (k 3, 0) k 2 < 100? k 3 < k 2+1 SSA & Opts return 1 © Seth Copen Goldstein & Todd C. Mowry 2001 -3 Small problem. 32

Finding the CDG Y is control-dependent on X if • X branches to u

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 33

1 2 3 4 Dominance Frontier & path-convergence 1 5 6 9 7 8

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

Finding the CDG Y is control-dependent on X if • X branches to u

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. u X v u X © Seth Copen Goldstein & Todd C. Mowry 2001 -3 v Y exit Y v Y SSA & Opts exit u src X 35

Finding the CDG • • SSA & Opts Construct CFG Add entry node and

Finding the CDG • • SSA & Opts 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) © Seth Copen Goldstein & Todd C. Mowry 2001 -3 36

CDG of example B 0 i 0<-0 exit j 0<-0 B 1 j 1

CDG of example B 0 i 0<-0 exit j 0<-0 B 1 j 1 (j 0, j 2) i 1 (i 0, i 2) i 2<-i 1*2 2 2 1 1 j 2<-j 1+1 entry j 2<10? B 2 return j 2 0 0 entry SSA & Opts © Seth Copen Goldstein & Todd C. Mowry 2001 -3 37

CDG of example exit 2 2 1 1 0 entry exit: {} 2: {entry}

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