static single assignment cs 540. spring 2010 2 efficient representations for reachability efficiency...

33
Static Single Assignment CS 540

Upload: gideon-hamsher

Post on 14-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Static Single Assignment

CS 540

CS540Spring 2010

2

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)

CS540Spring 2010

3

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.

CS540Spring 2010

4

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.

CS540Spring 2010

5

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.

CS540Spring 2010

6

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

CS540Spring 2010

7

SSA for Structured Code

Associate with each variable x, a current counter xc.

Assignment statement: x := y op z

becomes

xxc++ := yyc op zzc

CS540Spring 2010

8

SSA for Structured Code

Loops:Repeat S until Efor all variables M with definition k in loop body

if M has a definition j above the loop thengenerate MMc++ := f (Mk,Mj); at loop start

s = 1 s1 = 1repeat repeat

… … s3 = (s1,s2)

s = s + 1 s2 = s3 + 1until s > 5 until s2 > 5

CS540Spring 2010

9

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 MMc++ := (Mk, Mj); after the conditional

if … then if … then

a := b aj := bn

else else

a := c ak := cm

al = (aj,ak)

CS540Spring 2010

10

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 MMc++ := (Mk, Mj); after the conditional

a := c aj := cm

if … then if … then

a := b ak := bn

else … else … al =

(aj,ak)

CS540Spring 2010

11

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 MMc++ := (Mk, Mj); after the conditional

a := c aj := cm

if … then if … then

a := b ak := bn

al = (aj,ak)

CS540Spring 2010

12

i = j = k = l = 1repeat 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 + 6until T

CS540Spring 2010

13

i = j = k = l = 1repeat 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 + 6until T

i1 = j = k = l = 1repeat 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 + 6until T

Number existing defns

CS540Spring 2010

14

i = j = k = l = 1repeat 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 + 6until T

i1 = j = k = l = 1repeat 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 + 6until T

Add definitionswhere needed

CS540Spring 2010

15

i = j = k = l = 1repeat 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 + 6until T

i1 = j = k = l = 1repeat 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 + 6until T

Fill in theuse numbers

CS540Spring 2010

16

i = j = k = l = 1repeat 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 + 6until T

i = j1 = k = l = 1repeat 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 + 6until T

CS540Spring 2010

17

i = j = k = l = 1repeat 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 + 6until T

i = j = k1 = l = 1repeat 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 + 6until T

CS540Spring 2010

18

i = j = k = l = 1repeat 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 + 6until T

i = j = k = l1 = 1repeat 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 + 6until T

CS540Spring 2010

19

i = j = k = l = 1repeat

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 + 6until T

i1 = j1 = k1 = l1 = 1repeat 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 + 6until T

Using SSA for Constant Propagation

• For statements xi := C, for some constant C, replace all xi with C and remove the statement

• For xi := (c,c,...,c), for some constant c, replace statement with xi := c

• Can extend to evaluate conditional branches

• IterateLocates 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

TF

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

TF

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

TF

a1 := 3 d1 := 2

d3 = (2,2) a3 = (3,a2) f1 := a3 + d3 g1 := 5 a2 := 5 – d3 f1 <= 5

f2 := 5 + 1 5 < a2

f3 := (f1,f2)d2 := 2

T F

TF

Example: SSA a1 := 3 d1 := 2

d3 = (2,2) a3 = (3,a2) f1 := a3 + d3 g1 := 5 a2 := 5 – d3 f1 <= 5

f2 := 5 + 1 5 < a2

f3 := (f1,f2)d2 := 2

T F

TF

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

TF

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

TF

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

TF

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

TF

a1 := 3 d1 := 2

d3 = a3 = f1 := 3 + 2 g1 := 5 a2 := 3 f1 <= 5

f2 := 6

f3 := (f1,6)d2 := 2

T F

Example: SSA a1 := 3 d1 := 2

d3 = a3 = f1 := 3 + 2 g1 := 5 a2 := 3 f1 <= 5

f2 := 6

f3 := (f2)d2 := 2

T F

a1 := 3 d1 := 2

d3 = a3 = f1 := 3 + 2 g1 := 5 a2 := 3true

f2 := 6

f3 := (6)d2 := 2

Example: SSA

a1 := 3 d1 := 2

d3 = a3 = f1 := 5 g1 := 5 a2 := 3true

f2 := 6

f3 := (6)d2 := 2

a1 := 3 d1 := 2

d3 = a3 = f1 := 3 + 2 g1 := 5 a2 := 3

f2 := 6

d2 := 2

CS540Spring 2010

28

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

X17 Ø(x10,x11)

... x17

... ...

... x17

X17 x10 X17 x11

*

CS540Spring 2010

29

Exit

k0 =k1 = k0

k4 =(k2,k3)

k1 =(k0,k4)

k3 =k1 + 2k4 = k3

k2 =k1 + 1k4 = k2

i = j = k0 = l = 1k1 = k0repeat 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 = k4until T

k1 =k4

CS 540 Spring 2010 GMU 30

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 GMU 31

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 GMU 32

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 GMU 33

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.