Title: Static Single Assignment Form (SSA)
1Static Single Assignment Form (SSA)
- Andrew McCreight
- (based on Prof. Shaos slides)
- CS421
- 11/18/05
2Improving 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 lets extend the basic CFG
3Static 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 F(v3,v4) u
v5 y
SSA transformation
4F-Functions
- To support this special F-functions are added to
each branch join point - F(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
F(v3,v4) u v5 y
if P then v 4 else v 6 u v y
SSA transformation
5Dominance
- 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 F(v3,v4) u v5 y
6What it buys us
- Easy to determine def of each use theres 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 F(v3,v4) u
v5 y
SSA transformation
use-def chains are much simpler
7More efficiency
- Weve 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
8Whats the catch?
- More variables
- Increase in code size due to F-functions
- But only linearly (in practice, SSA is 0.6-2.4
times larger) - Some optimizations are more annoying (things that
copy blocks) - But on the whole, a win for compilers
9Sparse Analysis
- Some properties are global
- v1 is always 4, everywhere in the program
- Dont 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 F(v3,v4) u
v5 y
10Sparse 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 dont 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 F(v3,v4) u
v5 y
11Sparse 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 cant, do both branches
- Repeat, until no more blocks to examine.
12Construction
- First, decide what variables need F-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
13Placing F Functions
- Need a F 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)
14De-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