Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static Single Assignment Form (SSA)

Similar presentations


Presentation on theme: "Static Single Assignment Form (SSA)"— Presentation transcript:

1 Static Single Assignment Form (SSA)
Andrew McCreight (based on Prof. Shao’s slides) CS421 11/18/05

2 Improving Compiler Efficiency
Many optimization passes Lots of time spent optimizing Worth it to have new IR for opt. translation to/from new IR (cost) easier/faster opt. (benefit) so let’s extend the basic CFG

3 Static Single Assignment
Main idea: each assignment has a unique name v := 4 z := v + 5 v := 6 y := v + 7 if P then v := 4 else v := 6 u := v + y v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y SSA transformation

4 Φ-Functions To support this: special Φ-functions are added to each branch join point Φ(v1, v2, …) is equal to vi, if we take the i-th predecessor to get to it if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y if P then v := 4 else v := 6 u := v + y SSA transformation

5 Dominance Also: a def must dominate all of its uses
Statement a dominates statement b if along all valid paths from the entry to the exit, you reach a before you reach b. v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y

6 What it buys us Easy to determine def of each use: there’s only one!
Less need for data flow analysis v := 4 z := v + 5 v := 6 y := v + 7 if P then v := 4 else v := 6 u := v + y v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y SSA transformation use-def chains are much simpler

7 More efficiency We’ve basically pre-computed some dataflow information, which allows us to avoid redoing it with multiple passes Before, there were a quadratic number of use-def chains (in terms of edges) Now, there are a linear number of them Asymptotic efficiency improvement

8 What’s the catch? More variables
Increase in code size due to Φ-functions But only linearly (in practice, SSA is times larger) Some optimizations are more annoying (things that copy blocks) But on the whole, a win for compilers

9 Sparse Analysis Some properties are global
v1 is always 4, everywhere in the program Don’t have to track the value of v at each point This means it is sparse v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y

10 Sparse Constant Prop Keep a global table of things that are const
At each instruction, check each use to see if it is constant. If you don’t know yet, find out, save the answer. Only have to examine each def once. At phi, only const if all args are same const v1 := 4 z := v1 + 5 v2 := 6 y := v2 + 7 if P then v3 := 4 else v4 := 6 v5 := Φ(v3,v4) u := v5 + y

11 Sparse conditional const prop
We can be more clever, by evaluating branches. Track which blocks are run. First, assume only first block is run Ignore vals from unrun blocks If ends with a jump, add next block, check it If ends with a branch, see if we can figure out result of test. If we can, only do taken branch next. If we can’t, do both branches Repeat, until no more blocks to examine.

12 Construction First, decide what variables need Φ-functions, and where
Next, rename the variables First step is harder For second, just give each assignment a unique name, do a reaching definitions kind of analysis

13 Placing Φ Functions Need a Φ function at every join point where two different defs of the same variable reach Want to minimize the number of them Can be computed efficiently (see Appel ch. 19)

14 De-SSA Eventually, we want to leave SSA Drop all subscripts
Simplest way: convert all phi statements to move instructions (in predecessor block) Many of these moves can be eliminated if you are more clever, or through later optimization


Download ppt "Static Single Assignment Form (SSA)"

Similar presentations


Ads by Google