Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static Single Assignment

Similar presentations


Presentation on theme: "Static Single Assignment"— Presentation transcript:

1 Static Single Assignment
Lecture 15 CS 5363 Programming Languages and Compilers

2 Outline SSA Form Benefit SSA Translation SSA Back Translation

3 What is SSA Program is in SSA form if every variable is only assigned once Goal: simplify and accelerate optimization process

4 Example: Sequence Easy to do for sequential programs: SSA Original
a := b + c b := c + 1 d := b + c a := a + 1 e := a + b a1 := b1 + c1 b2 := c1 + 1 d1 := b2 + c1 a2 := a1 + 1 e1 := a2 + b2 SSA form makes clear that a2 is not the same as a1, so easier for analysis

5 Example: Condition Conditions: what to do on control-flow merge?
Original SSA if B then a := b else a := c end … a … if B then a1 := b else a2 := c End … a? … is it a1 or is it a2?

6 Solution: -Function Conditions: what to do on control-flow merge?
Original SSA if B then a := b else a := c end … a … if B then a1 := b else a2 := c End a3 := (a1,a2)  selects between a1 and a2

7 The -Function -functions are always at the beginning of a basic block Select between values depending on control-flow a1 := (a1…ak): the block has k preceding blocks PHI functions evaluated simultaneously within one basic block

8 SSA and CFG SSA is normally done for control-flow graphs (CFG)
Basic blocks are in 3-address form

9 Review: Control flow graph
A CFG models transfer of control in a program nodes are basic blocks (straight-line blocks of code) edges represent control flow (loops, if/else, goto …) if x = y then S1 else S2 end S3

10 SSA: a Simple Example if B then a1 := 1 else a2 := 2 End
a3 := PHI(a1,a2) … a3 …

11 Repeat: IR front end produces IR
optimizer transforms IR to more efficient program back end transform IR to target code

12 SSA as IR Current trend in compiler community is to use SSA as *the* IR for everything in back end. (NB: for compilers that generate machine code, not those that generate bytecode.)

13 Why SSA? Making the optimization analysis much simpler
We will revisit the common expression and liveness analysis

14 Common Expression Revisited
c = a + b; c1 = a1 + b1; a = b + c; a2 = b1 + c1; d = a + b; d1 = a2 + b1;

15 Liveness Revisted c = a + b; c1 = a1 + b1; … a = b + c; a2 = b1 + c1;
d = a + b; d1 = a2 + b1;

16 Transform to SSA Two steps: Where to place -functions?
Rename Variables Where to place -functions? We want minimal amount of needed  Save memory Algorithms will work faster 16

17 Path Convergence Criterion
There should be a  for a at node Z if: There is a block X containing a definition of a. There is a block Y (Y != X) containing a definition of a. There is a nonempty path Pxz of edges from X to Z.

18 Path Convergence Criterion
There should be a  for a at node Z if: 4. There is a nonempty path Pyz of edges from Y to Z. 5. Path Pxz and Pyz do not have any nodes in common other than Z 6. The node Z does not appear within both Pxz and Pyz prior to the end (although it may appear in one or the other)

19 Iterated Path-Convergence
Inserted  is itself a definition! While there are nodes X,Y,Z satisfying conditions 1-5 and Z does not contain a phi-function for a do insert PHI at node Z.

20 Example (Simple) 1. block X containing a definition of a
2. block Y (Y != X) containing a definition of a. 3. path Pxz of edges from X to Z. 4. path Pyz of edges from Y to Z. 5. Path Pxz and Pyz do not have any nodes in common other than Z 6. The node Z does not appear within both Pxz and Pyz prior to the end

21 Dominance Property of SSA
Dominance: node D dominates node N if every path from the start node to N goes through D. Dominance is a property of basic blcoks. (one Node dominates a set of nodes). For the dominance property, „definition of x“ thus means the basic block in which x is defined.

22 Example 5 Dominates all nodes in the gray area

23 Dominance and SSA Creation
Dominance can be used to efficiently build SSA -Functions are placed in all basic blocks of the Dominance Frontier. Dominance Frontier: the set of all nodes N such that D: dominates an immediate predecessor of N but does not dominate N.

24 Dominance and SSA Creation
DF(D) = the set of all nodes N such that D dominates an immediate predecessor of N but does not strictly dominate N. Intuition: Nodes at the border of a region of dominance

25 Example 5 Dominates all nodes in the gray area

26 Dominance and SSA Creation
Targets of edges from the dominates by 5 to the region not strictly dominated by 5. DF(5)= {4, 5, 12, 13}

27 Simple Example DF(B1)= DF(B2)= DF(B3)= DF(B4)=

28 Simple Example DF(B1)={?} DF(B2)= DF(B3)= DF(B4)=

29 Simple Example DF(B1)={} DF(B2)= DF(B3)= DF(B4)=

30 Simple Example DF(B1)={} DF(B2)={?} DF(B3)= DF(B4)=

31 Simple Example DF(B1)={} DF(B2)={B4} DF(B3)= DF(B4)=

32 Simple Example DF(B1)={} DF(B2)={B4} DF(B3)={B4} DF(B4)=

33 Simple Example DF(B1)={} DF(B2)={B4} DF(B3)={B4} DF(B4)={}

34 Simple Example DF(B1)={} DF(B2)={B4} DF(B3)={B4} DF(B4)={}
PHI-Function needed in B4 (for a)

35 Properties of SSA Simplifies many optimizations Examples:
Every variable has only one definition Every use knows its definition, every definition knows its uses Unrelated variables get different names Examples: Constant propagation Value numbering Invariant code motion and removal Strength reduction Partial redundancy elimination

36 SSA in the Real World Invented end of the 80s, a lot of research in the 90s Used in many modern compilers ETH Oberon 2 LLVM GNU GCC 4 IBM Jikes Java VM Java Hotspot VM Mono Many more…

37 Transforming out-of SSA
Processor cannot execute -Function How do we remove it?

38 Simple Copy Placement

39 Next Week Advanced Topics Dynamic dispatch
Functional Programming and High Order Functions


Download ppt "Static Single Assignment"

Similar presentations


Ads by Google