180 likes | 290 Views
Some Properties of SSA. Mooly Sagiv. Outline. Why is it called Static Single Assignment form What does it buy us? How much does it cost us? Open questions. Static Single Assignment. Every variable is assigned once But in loops a variable will be assigned many times. An Acyclic Example.
E N D
Some Properties of SSA Mooly Sagiv
Outline • Why is it called Static Single Assignment form • What does it buy us? • How much does it cost us? • Open questions
Static Single Assignment • Every variable is assigned once • But in loops a variable will be assigned many times
An Acyclic Example x x + 2 x > 0 x x + 1 x x + 4 x > 0 x x + 3 x x + 6 x x + 5
A Cyclic Example i = 1; j = 1; k = 0 while (k < 0) { if (j < 0) { j = i; k = k + 1; } else { j = k ; k = k + 2 } } return j
What does it buy us? • Compact representation of DU chains • Convert “imperative” into “functional” style • Extended basic blocks • Allows flow-insensitive analysis
How much does it cost • Code size • Worst case the extra number of assignments is quadratic • For programs with while-do and if-then-else linear • In practice linear • Construction of SSA
Open Questions • Dealing with pointers • When is it useful? • Is it really needed? • Can we choose an appropriate data structure in the iterative data flow algorithm instead
Conditional Constant Propagation • Conditions with constant values can be interpreted to improve precision • A more precise solution is obtained “optimistically”
char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3 );} main() { printf(“%s\n”, “red”);} “red”
char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3);}
Iterative Data-Flow Algorithm Input:aflow graph G=(N,E,r) An init value Init A montonic function FB for every B in N Output:For every N in(N) Initializatio: in(Entry) := Init; for each node B in N-{Entry} do in(B) := WL := N - {Entry} Iteration: while WL != {} do Select and remove an B from WL out := FB(in(B)) For all B’ in succ(B) such that in(B’) != in(B’) out do in(B’):= in(B’) out WL := WL {B’}
Iterative Data-Flow Algorithm Input:aflow graph G=(N,E,r) An init value Init A montonic function FB for every B in N Output:For every N in(N) Initializatio: in(Entry) := Init; for each node B in N-{Entry} do in(B) := WL := {Entry} Iteration: while WL != {} do Select and remove an B from WL out := FB(in(B)) For all B’ in succ(B) such that in(B’) != in(B’) out do in(B’):= in(B’) out WL := WL {B’}
char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3);}
Conditional Constant Propagation • initialize the worklist to the entry node • mark all edges as not executable • repeat until the worklist is empty: • select and remove a node from the worklist • if it is an assignment then mark the successor edge as executable • if it is a test then symbolically evaluate the test and mark the enabled successor edges as executable • if test evaluates to true or mark true edge executable • if test evaluates to false or mark false edge executable • update the value of all the variables at the entry and exit of this node • if there are changes then add all successors reachable from the node with edges marked executable to the worklist
Sparse Conditional Constant • bring the program in SSA form • initialize the analysis information: • all variables are mapped to • all flow edges are marked as not executable • initialize the two worklists • Flow-Worklist contains all edges of the flow graph with the entry node as source • SSA-Worklist is empty
repeat until both worklists are empty: • select and remove an edge from one of the worklists • if it is a flow edge then • if the edge is not marked executable then • mark it executable • if the target of the edge is a f-node then call visit-f • if it is the first time the node is visited (only one incoming flow edge is marked executable) and it is a normal node then call visit-instr • if it is an SSA edge then • if the target of the edge is a f-node then call visit-f • if it is a normal node and at least one of the flow edges entering the node are marked executable then call visit-instr
visit-f: (the node is a f-node) • the assigned variable is given a value that is the join the values of the arguments with incoming edges markedexecutable • visit-instr: (the node is a normal node) • determine the value of the expression of the node and update the variable in case of an assignment • if there are changes then • if the node is an assignment then add all SSA edges with source at the target of the current edge to the SSA-worklist • if the node is a test then add all relevant flow edges to the Flow-worklist and mark them executable • if test evaluates to true or add true edge • if test evaluates to false or : add false edge