topic 16: static single assignment -...

42
Topic 16: Static Single Assignment Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1

Upload: vuliem

Post on 11-Mar-2018

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Topic 16: Static Single Assignment

Compiler Design

Prof. Hanjun Kim

CoreLab (Compiler Research Lab)

POSTECH

1

Page 2: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Def-Use Chains, Use-Def Chains

• Many optimization need to find all use-sites for each definition, and all definition-sites for each use

• Constant propagation: refer to the definition-site of the unique reaching definition

• Copy propagation, common sub-expression elimination, …

• 𝑑𝑒𝑓 − 𝑢𝑠𝑒 𝑐ℎ𝑎𝑖𝑛• For each definition 𝑑 of 𝑟, list of pointers to all uses of 𝑟

that 𝑑 reaches

• 𝑢𝑠𝑒 − 𝑑𝑒𝑓 𝑐ℎ𝑎𝑖𝑛• For each use 𝑢 of 𝑟, list of pointers to all definitions of 𝑟

that reach 𝑢

2

Page 3: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Node OUT IN

1 1

2 1,2 1

3 1,2 1,2

4 1,4 1,2,4

5 1,4 1,4

6 1,4,6 1,4

7 4,6,7 1,4,6

8 4,6,7 4,6,7

r1 = 5

r3 = 1

Br r3>r1, 6

r3 = r3 + 1

1:

2:

3:

4:

goto 35:

r4 = 10

r1 = r1 + r4

M[r3] = r1

6:

7:

8:

Def-Use Chains, Use-Def Chains

Reaching Definition

Page 4: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Static Single Assignment

• Static Single Assignment (SSA)• Improvement on def-use chains

• Each register has only one definition in program

• For each use 𝑢 of 𝑟, only one definition of 𝑟 reaches 𝑢

4

r1 = 5

r1 = r1 + 1

r2 = r1 + 1 r3 = r1 - 1

r1 = 5

r4 = r1 + 1

r2 = r4 + 1 r3 = r4 - 1

Page 5: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Why SSA?

• Static Single Assignment Advantages• Dataflow analysis and code optimization made simpler

• Variables have only one definition – no ambiguity

• Dominator information is encoded in the assignment

• Less space required to represent def-use chain

• For each variable, space is proportional to uses * defs

• Defs = 1 in SSA

• Eliminates unnecessary relationshipsfor i = 1 to N do A[i] = 0

for i = 1 to N do A[i] = 0

• No reason why both loops should be forced to use same register to hold index register

• SSA renames second i to a new register which may lead to better register allocation / optimization

5

Page 6: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Phi Function

6

r1 = 5

r2 = r1 + 1

r3 = r1 + 1 r3 = r1 - 1

r4 = r3 * 4

r1 = 5

r2 = r1 + 1

r3 = r1 + 1 r3’ = r1 - 1

r4 = ? * 4

Page 7: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Phi Function

• 𝑟 = ∅(𝑟′, 𝑟")• Set of move operations on each incoming edge

• It enables the use of 𝑟 to be reached by exactly one definition of 𝑟

• 𝑟 = 𝑟′ if control enters from left

• 𝑟 = 𝑟" if control enters from right

7

Page 8: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Phi Function

8

r1 = 5

r2 = r1 + 1

r3’ = r1 + 1 r3” = r1 - 1

r3=𝜙(r3’,r3”)r4 = r3 * 4

Page 9: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Conversion to SSA Form

• Where to insert a 𝜙-function?• Insert a 𝜙-function for a register 𝑟 at node 𝑧 of the flow graph if ALL

of the following are true:

1. There is a block 𝑥 contacting a definition of 𝑟

2. There is another block 𝑦 (𝑦 ≠ 𝑥) contacting a definition of 𝑟

3. There is a non-empty path 𝑃𝑥𝑧 of edges from 𝑥 to 𝑧

4. There is a non-empty path 𝑃𝑦𝑧 of edges from 𝑦 to 𝑧

5. Paths 𝑃𝑥𝑧 and 𝑃𝑦𝑧 do not share any node other than 𝑧

6. The node 𝑧 does not appear within both 𝑃𝑥𝑧 and 𝑃𝑦𝑧 prior to the end, though it may appear in one or the other

• Assume CFG entry node contains implicit definition of every variable

• The variable is either a formal parameter or uninitialized

• 𝜙-function is considered as a definition

9

Page 10: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Conversion to SSA Form

• Iterated path-convergence algorithm

while(there are nodes 𝑥, 𝑦, 𝑧 satisfying conditions 1-6)

&& (𝑧 does not contain a 𝜙-function for 𝑟)do

insert 𝑟 = 𝜙(𝑟, 𝑟, … , 𝑟) (one per predecessor) at node 𝑧

• Costly to compute every triple of nodes 𝑥, 𝑦, 𝑧 and every path from 𝑥 and 𝑦

• Any solution?• Definitions dominate uses – it will simplify computation!!

10

Page 11: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Dominance Frontier

• Dominance Frontier of node 𝑥• Set of all nodes 𝑤 such that 𝑥 dominates a predecessor

of 𝑤, but does not strictly dominate 𝑤

• Strict Domination• 𝑥 strictly dominates 𝑤 if 𝑥 dominates 𝑤 and 𝑥 ≠ 𝑤

• Dominance Frontier Criterion• Whenever node 𝑥 contains definition of some register 𝑟,

then any node 𝑧 in the dominance frontier of 𝑥 needs 𝜙-function for 𝑟

• Iterated Dominance Frontier• Since a 𝜙-function itself is a definition, we must

repeatedly apply the dominance frontier

11

Page 12: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Dominance Frontier Example

12

1

2

3

4

5

6

8

7

9

11

10

Which nodes are in the dominance frontier of node 5?

Strictly Dominated

Dominance Frontier

Page 13: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Dominance Frontier Computation

𝐷𝐹 𝑛 = 𝐷𝐹𝑙𝑜𝑐𝑎𝑙 𝑛 ∪ ∪𝑐∈𝑐ℎ𝑖𝑙𝑑𝑟𝑒𝑛 𝑛 𝐷𝐹𝑢𝑝[𝑐]

• 𝐷𝐹[𝑛]: dominance frontier of 𝑛

• 𝐷𝐹𝑙𝑜𝑐𝑎𝑙[𝑛]: successors of 𝑛 in CFG that are not strictly dominated by 𝑛

• 𝐷𝐹𝑢𝑝[𝑐]: nodes in dominance frontier of 𝑐 that are not strictly dominated by 𝑐’s immediate dominator

• 𝑐ℎ𝑖𝑙𝑑𝑟𝑒𝑛 𝑛 : the nodes whose idom is n

• Use dominator tree• Work bottom up in dominator tree

13

Page 14: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Example

14

Node 𝐷𝑜𝑚[𝑛] 𝐼𝐷𝑜𝑚[𝑛]

1 1

2 1,2 1

3 1,2,3 2

4 1,2,3,4 3

5 1,2,3,4,5 4

6 1,2,3,4,6 4

7 1,2,3,4,5,7 5

8 1,2,3,4,5,7,8 7

9 1,2,3,4,5,9 5

10 1,2,3,4,5,9,10 9

11 1,2,3,4,5,11 5

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r2<205:

r2 = r1

r3 = r3 + 1

r2 = r37:

8:

9:

r3 = r3 + 210:

br r3<100

4:

11:

return r26:

Page 15: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

Dominator Analysis

• If 𝑑 dominates each of the 𝑝𝑖, then 𝑑 dominates 𝑛

• If 𝑑 dominates 𝑛, then 𝑑 dominates each of the 𝑝𝑖• 𝐷𝑜𝑚[𝑛] = set of nodes that dominate node n

• 𝑁 = set of all nodes

• Computation:1. 𝐷𝑜𝑚 𝑠0 = 𝑠02. for 𝑛 ∈ 𝑁 − {𝑠0} do 𝐷𝑜𝑚 𝑛 = 𝑁

3. While (changes to any 𝐷𝑜𝑚 𝑛 occur) dofor 𝑛 ∈ 𝑁 − 𝑠0 do𝐷𝑜𝑚 𝑛 = 𝑛 ∪ ( ∩ 𝑝∈𝑝𝑟𝑒𝑑 𝑛 𝐷𝑜𝑚 𝑝 )

15

Page 16: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Example

16

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r2<205:

r2 = r1

r3 = r3 + 1

r2 = r37:

8:

9:

r3 = r3 + 210:

br r3<100

4:

11:

return r26:

Dominator Tree:1

2

6

7

3

10

4

8

5

119

Page 17: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Example

17

Dominance Frontier:

1

2

6

7

3

10

4

8

5

119

Node 𝐷𝐹𝑙𝑜𝑐𝑎𝑙[𝑛] 𝐷𝐹𝑢𝑝 [𝑛] 𝐷𝐹 [𝑛]

1 - - -

2 - - -

3 - - -

4 - - 4

5 - 4 4

6 - - -

7 - - 11

8 11 11 11

9 - - 11

10 11 11 11

11 4 4 4

Page 18: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Example

18

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r2<205:

r2 = r1

r3 = r3 + 1

r2 = r37:

8:

9:

r3 = r3 + 210:

br r3<100

4:

r2 = 𝜙(r2,r2)r3 = 𝜙(r3,r3)

11:

return r26:

Insert 𝜙-functions :

Node 𝐷𝐹 [𝑛]

1 -

2 -

3 -

4 4

5 4

6 -

7 11

8 11

9 11

10 11

11 4

Dominance Frontier CriterionWhenever node 𝑥 contains definition of some register 𝑟, any node 𝑧 in the dominance frontier of 𝑥 needs 𝜙-function for 𝑟

Page 19: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Example

19

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r2<205:

r2 = r1

r3 = r3 + 1

r2 = r37:

8:

9:

r3 = r3 + 210:

r2 = 𝜙(r2,r2)r3 = 𝜙(r3,r3)br r3<100

4:

r2 = 𝜙(r2,r2)r3 = 𝜙(r3,r3)

11:

return r26:

Insert 𝜙-functions :

Node 𝐷𝐹 [𝑛]

1 -

2 -

3 -

4 4

5 4

6 -

7 11

8 11

9 11

10 11

11 4

Dominance Frontier CriterionWhenever node 𝑥 contains definition of some register 𝑟, any node 𝑧 in the dominance frontier of 𝑥 needs 𝜙-function for 𝑟

Page 20: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Example

• Rename Variables1. Traverse dominator tree, renaming different definitions

of 𝑟 to 𝑟1, 𝑟2, 𝑟3, …

2. Rename each regular use of 𝑟 to most recent definition of 𝑟

3. Rename 𝜙-function arguments with each incoming edge’s unique definition

20

Page 21: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Example

21

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r21<205:

r22 = r1

r32 = r31+1

r23 = r317:

8:

9:

r33 = r31+210:

r21=𝜙(r2,r24)r31=𝜙(r3,r34)br r31<100

4:

r24= 𝜙 (r22,r23)

r34= 𝜙 (r32,r33)

11:

return r216:

Rename Variables:

Page 22: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Dominance Property

• Dominance property of SSA form:• Definitions dominate uses

• If 𝑥 is used in non-𝜙-statement in node 𝑛, then definition of 𝑥 dominates 𝑛

• If 𝑥 is 𝑖𝑡ℎ argument of 𝜙-function in node 𝑛, then definition of 𝑥 dominates 𝑖𝑡ℎ predecessor of 𝑛

22

Page 23: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Dead Code Elimination

Given d: t = x op y

• t is live at end of node d if there exists a path from the end of d to use of t that does not go through definition of t

• If program is not in SSA form• Liveness analysis is required to determine if t live at the

end of d

• If program is in SSA form• There is only one definition of t• If there exists use of t, there is a path from the end of d

to use• Every use has a unique definition

• t is live at end of node d if t is used at least once

23

Page 24: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Dead Code Elimination

• Algorithmwhile (for each temporary t with no uses &&

statement defining t has no side-effect)

do

delete statement definition t

24

r1 = 5

r2 = 10

1:

2:

br r3 > r23:

r21 = r2+15

r41 = r3+1

r42 = r34:

5:

6:

r22=𝜙(r21,r2)r4=𝜙(r41,r42)

8:

7:

M[r4]=r22

Page 25: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Simple Constant Propagation

Given d: t = c, c is constant Given u: x = t op b

• If program is not in SSA form• Reaching definition analysis is required

• Use of t in u may be replaced by c if d reaches u and no other definition of t reaches u

• If program is in SSA form• d is only one definition of t and definitions dominate

uses, so d reaches u without any other definition • Any use of t can be replaced by c

• Any 𝜙-function of form v = 𝜙(𝑐1, 𝑐2, … , 𝑐𝑛) where 𝑐𝑖 = 𝑐 can be replaced by v = c

25

Page 26: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Simple Constant Propagation

26

r2 = 102:

br r3 > r23:

r21 = r2+15

r41 = r3+1

r42 = r34:

5:

6:

r22=𝜙(r21,r2)r4=𝜙(r41,r42)

8:

7:

M[r4]=r22

Page 27: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

• r2 always has value of 1

• Nodes 9, 10 never executed

• “simple” constant propagation algorithm

• Assumes nodes 9, 10 may be executed (through reaching definitions analysis)

• Cannot optimize use of r2 in node 5 since definitions 7 and 9 both reach 5

27

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r21<205:

r22 = r1

r32 = r31+1

r23 = r317:

8:

9:

r33 = r31+210:

r21=𝜙(r2,r24)r31=𝜙(r3,r34)br r31<100

4:

r24= 𝜙 (r22,r23)

r34= 𝜙 (r32,r33)

11:

return r216:

Page 28: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

Much smarter than “simple” constant propagation required

• Does not assume a node can execute until evidence exists

• Does not assume a register is non-constant unless evidence exists

Track runtime value of each register 𝑟 using lattice of values

• 𝑉 𝑟 =⊥ (bottom): compiler has seen no evidence that any assignment to 𝑟is ever executed

• 𝑉 𝑟 = 4: compiler has seen evidence that an assignment 𝑟 = 4 is executed, but has seen no evidence that 𝑟 is ever assigned to another value

• 𝑉 𝑟 = ⊤ (top): compiler has seen evidence that 𝑟 will have, at various time, two different values or some value that is not predictable at compile-time

Also

• All registers start at bottom of lattice

• New information can only move registers up in lattice

28

Page 29: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

Track executability of each node in 𝑁• 𝐸 𝑁 = 𝑓𝑎𝑙𝑠𝑒: compiler has seen no evidence that node 𝑁 can be executed

• 𝐸 𝑁 = 𝑡𝑟𝑢𝑒: compiler has seen evidence that node 𝑁can be executed

Initially,• 𝑉 𝑟 =⊥, for all register 𝑟

• 𝐸 𝑠0 = 𝑡𝑟𝑢𝑒, 𝑠0 is CFG start node

• 𝐸 𝑁 = 𝑓𝑎𝑙𝑠𝑒, for all CFG nodes 𝑁 ≠ 𝑠0

29

Page 30: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

Algorithm: apply following conditions until no more changes occur to 𝐸 or 𝑉 values

1. Given: register 𝑟 with no definition (formal parameter, uninitialized)Action: 𝑉 𝑟 =⊥

2. Given: executable node 𝐵 with only one successor 𝐶Action: 𝐸 𝐶 = 𝑡𝑟𝑢𝑒

3. Given: executable assignment 𝑟 = 𝑥 𝑜𝑝 𝑦, 𝑉 𝑥 = 𝑐1 and 𝑉 𝑦 = 𝑐2Action: 𝑉 𝑟 = 𝑐1 𝑜𝑝 𝑐2

4. Given: executable assignment 𝑟 = 𝑥 𝑜𝑝 𝑦, 𝑉 𝑥 = ⊤ or 𝑉 𝑦 = ⊤Action: 𝑉 𝑟 = ⊤

5. Given: executable assignment 𝑟 = 𝑀[. . ] or r = f(. . )Action: 𝑉 𝑟 = ⊤

6. Given: executable assignment 𝑟 = 𝜙(𝑥1, 𝑥2, … , 𝑥𝑛), 𝑉 𝑥𝑖 = 𝑐1, 𝑉 𝑥𝑗 = 𝑐2, and predecessors 𝑖 and 𝑗 are executableAction: 𝑉 𝑟 = ⊤

7. Given: executable assignment 𝑟 = 𝜙(𝑥1, 𝑥2, … , 𝑥𝑛), 𝑉 𝑥𝑖 = ⊤, and predecessor 𝑖 is executableAction: 𝑉 𝑟 = ⊤

30

Page 31: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

8. Given: executable assignment 𝑟 = 𝜙(𝑥1, 𝑥2, … , 𝑥𝑛), 𝑉 𝑥𝑖 = 𝑐𝑖, and predecessor 𝑖 is executable; and for all 𝑗 ≠ 𝑖, predecessor 𝑗 is not executable, or 𝑉 𝑥𝑗 =⊥,

or 𝑉 𝑥𝑗 = 𝑐𝑖Action: 𝑉 𝑟 = 𝑐𝑖

9. Given: executable branch 𝑏𝑟 𝑥 𝑏𝑜𝑝 𝑦, 𝐿1 (𝑒𝑙𝑠𝑒 𝐿2), 𝑉 𝑥 = ⊤ or 𝑉 𝑦 = ⊤Action: 𝐸 𝐿1 = 𝑡𝑟𝑢𝑒, 𝐸 𝐿2 = 𝑡𝑟𝑢𝑒

10.Given: executable branch 𝑏𝑟 𝑥 𝑏𝑜𝑝 𝑦, 𝐿1 (𝑒𝑙𝑠𝑒 𝐿2), 𝑉 𝑥 = 𝑐1 and 𝑉 𝑦 = 𝑐2Action: 𝐸 𝐿1 = 𝑡𝑟𝑢𝑒 or 𝐸 𝐿2 = 𝑡𝑟𝑢𝑒 depending on 𝑐1 𝑏𝑜𝑝 𝑐2

31

Page 32: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

Given 𝑉, 𝐸 values, program can be optimized as follows• If 𝐸 𝐵 = 𝑓𝑎𝑙𝑠𝑒, delete node 𝐵 from CFG

• If 𝑉 𝑟 = 𝑐, replace each use of 𝑟 by 𝑐, delete assignment to 𝑟

32

Page 33: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

33

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r21<205:

r22 = r1

r32 = r31+1

r23 = r317:

8:

9:

r33 = r31+210:

r21=𝜙(r2,r24)r31=𝜙(r3,r34)br r31<100

4:

r24= 𝜙 (r22,r23)

r34= 𝜙 (r32,r33)

11:

return r216:

𝑁 𝐸[𝑁]

1 T

2 F

3 F

4 F

5 F

6 F

7 F

8 F

9 F

10 F

11 F

𝑟 V[𝑟]

r1 ⊥

r2 ⊥

r21 ⊥

r22 ⊥

r23 ⊥

r24 ⊥

r3 ⊥

r31 ⊥

r32 ⊥

r33 ⊥

r34 ⊥

Page 34: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

34

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r21<205:

r22 = r1

r32 = r31+1

r23 = r317:

8:

9:

r33 = r31+210:

r21=𝜙(r2,r24)r31=𝜙(r3,r34)br r31<100

4:

r24= 𝜙 (r22,r23)

r34= 𝜙 (r32,r33)

11:

return r216:

𝑁 𝐸[𝑁]

1 T

2 T

3 T

4 T

5 F

6 F

7 F

8 F

9 F

10 F

11 F

𝑟 V[𝑟]

r1 1

r2 1

r21 ⊥

r22 ⊥

r23 ⊥

r24 ⊥

r3 0

r31 ⊥

r32 ⊥

r33 ⊥

r34 ⊥

Page 35: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

35

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r21<205:

r22 = r1

r32 = r31+1

r23 = r317:

8:

9:

r33 = r31+210:

r21=𝜙(r2,r24)r31=𝜙(r3,r34)br r31<100

4:

r24= 𝜙 (r22,r23)

r34= 𝜙 (r32,r33)

11:

return r216:

𝑁 𝐸[𝑁]

1 T

2 T

3 T

4 T

5 T

6 F

7 F

8 F

9 F

10 F

11 F

𝑟 V[𝑟]

r1 1

r2 1

r21 1

r22 ⊥

r23 ⊥

r24 ⊥

r3 0

r31 0

r32 ⊥

r33 ⊥

r34 ⊥

Page 36: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

36

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r21<205:

r22 = r1

r32 = r31+1

r23 = r317:

8:

9:

r33 = r31+210:

r21=𝜙(r2,r24)r31=𝜙(r3,r34)br r31<100

4:

r24= 𝜙 (r22,r23)

r34= 𝜙 (r32,r33)

11:

return r216:

𝑁 𝐸[𝑁]

1 T

2 T

3 T

4 T

5 T

6 F

7 T

8 T

9 F

10 F

11 T

𝑟 V[𝑟]

r1 1

r2 1

r21 1

r22 1

r23 ⊥

r24 1

r3 0

r31 0

r32 1

r33 ⊥

r34 1

Page 37: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

37

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r21<205:

r22 = r1

r32 = r31+1

r23 = r317:

8:

9:

r33 = r31+210:

r21=𝜙(r2,r24)r31=𝜙(r3,r34)br r31<100

4:

r24= 𝜙 (r22,r23)

r34= 𝜙 (r32,r33)

11:

return r216:

𝑁 𝐸[𝑁]

1 T

2 T

3 T

4 T

5 T

6 T

7 T

8 T

9 F

10 F

11 T

𝑟 V[𝑟]

r1 1

r2 1

r21 1

r22 1

r23 ⊥

r24 1

r3 0

r31 ⊤

r32 1

r33 ⊥

r34 1

Page 38: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

38

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

br r21<205:

r22 = r1

r32 = r31+1

r23 = r317:

8:

9:

r33 = r31+210:

r21=𝜙(r2,r24)r31=𝜙(r3,r34)br r31<100

4:

r24= 𝜙 (r22,r23)

r34= 𝜙 (r32,r33)

11:

return r216:

𝑁 𝐸[𝑁]

1 T

2 T

3 T

4 T

5 T

6 T

7 T

8 T

9 F

10 F

11 T

𝑟 V[𝑟]

r1 1

r2 1

r21 1

r22 1

r23 ⊥

r24 1

r3 0

r31 ⊤

r32 ⊤

r33 ⊥

r34 ⊤

Page 39: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

39

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

r22 = r1

r32 = r31+1

7:

8:

r21=𝜙(r2,r24)r31=𝜙(r3,r34)br r31<100

4:

r24= 𝜙 (r22,r23)

r34= 𝜙 (r32,r33)

11:

return r216:

𝑁 𝐸[𝑁]

1 T

2 T

3 T

4 T

5 T

6 T

7 T

8 T

9 F

10 F

11 T

𝑟 V[𝑟]

r1 1

r2 1

r21 1

r22 1

r23 ⊥

r24 1

r3 0

r31 ⊤

r32 ⊤

r33 ⊥

r34 ⊤

Page 40: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

40

r1 = 1

r2 = 1

r3 = 0

1:

2:

3:

r22 = 1

r32 = r31+1

7:

8:

r21 = 1

r31=𝜙(r3,r34)br r31<100

4:

r24 = 1

r34= 𝜙 (r32,r33)

11:

return 16:

𝑁 𝐸[𝑁]

1 T

2 T

3 T

4 T

5 T

6 T

7 T

8 T

9 F

10 F

11 T

𝑟 V[𝑟]

r1 1

r2 1

r21 1

r22 1

r23 ⊥

r24 1

r3 0

r31 ⊤

r32 ⊤

r33 ⊥

r34 ⊤

Page 41: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

41

r3 = 03:

r32 = r31+18:

r31=𝜙(r3,r34)br r31<100

4:

r34= 𝜙 (r32,r33)

11:

return 16:

𝑁 𝐸[𝑁]

1 T

2 T

3 T

4 T

5 T

6 T

7 T

8 T

9 F

10 F

11 T

𝑟 V[𝑟]

r1 1

r2 1

r21 1

r22 1

r23 ⊥

r24 1

r3 0

r31 ⊤

r32 ⊤

r33 ⊥

r34 ⊤

Page 42: Topic 16: Static Single Assignment - POSTECHcorelab.postech.ac.kr/~hanjun/2017S_compiler/16_SSA.pdf · Topic 16: Static Single Assignment Compiler Design ... •Any solution?

SSA Conditional Constant Propagation

42

r3 = 03:

r32 = r31+18:

r31=𝜙(r3,r32)br r31<100

4:

return 16:

𝑁 𝐸[𝑁]

1 T

2 T

3 T

4 T

5 T

6 T

7 T

8 T

9 F

10 F

11 T

𝑟 V[𝑟]

r1 1

r2 1

r21 1

r22 1

r23 ⊥

r24 1

r3 0

r31 ⊤

r32 ⊤

r33 ⊥

r34 ⊤