Download presentation
Presentation is loading. Please wait.
1
CS 201 Compiler Construction
SSA-Based Sparse Conditional Constant Propagation
2
Conditional Constant Propagation
SSA-based algorithm is faster than the previous algorithm that was presented earlier. Basic Idea: Use SSA-edges for faster propagation of constants from definitions to uses.
3
Contd.. Handling Executable vs Non-Executable Edges
At ϕ-functions non-executable operands have lattice value of top-undef- This allows conditional constant propagation. Process nodes only when they are known to be executable. Non-executable SSA edges will transmit
4
Contd.. When can we process an SSA edge ?
When we know the destination of the edge is executable. Destination statement is executable when at least one of the incoming control flow edge is executable. Justification: In SSA form each use receives value from only one definition. Thus, no matter how we get to the use it will get the value from the same SSA-edge.
5
Contd.. Maintain two worklists: FlowWorkList (for control flow edges)
SSAWorkList (for SSA edges) Initialize: FlowWorkList Edges leaving the entry node. SSAWorkList {} ExecutableFlag(FlowEdges)False Lattice cells initialized to UNDEF or top Halt execution when both worklists are empty. Execution may proceed by processing edges from either worklist.
6
Contd.. 3. if e is CFG-edge from FlowWorkList then if ExecutableFlag(e) = false then ExecutableFlag(e) = true Perform Visitϕ for all ϕ-nodes at destination node. if only one incoming CFG-edges is TRUE (i.e., this is the first visit to the node) then Perform VisitExpression at the node if the node contains one outgoing CFGedge then add the edge to FlowWorkList
7
Contd.. 4. if e is SSA-edge from SSAWorkList then if destination of e is a ϕ-node then Perform Visitϕ elseif destination of e is an expression then examine ExecutableFlags for the CFG edges reaching that node and if any one flag is true then perform VisitExpression.
8
Contd.. Visitϕ For each operand in the ϕ-node Examine the ExecutableFlag of the corresponding CFG-edge and set the operand’s lattice value as follows: CFG-edge executable lattice value same as lattice value at the definition edge of the SSA-edge. CFG-edge non-executable lattice value is UNDEF (top). Compute the lattice value of the variable on the left hand side.
9
Contd.. VisitExpression Evaluate lattice value of the expression using lattice values of operands from places where they were defined. If lattice value changes then if expression is part of an assignment statement then add SSA-edge starting from left hand side to the SSAWorkList elseif expression is conditional/predicate then based upon the result add appropriate CFG-edges to the FlowWorkList: TRUEtrue edge; FALSEfalse edge; and bottom both edges.
10
Def-Use Edges vs SSA-edges
We cannot take advantage of non-executable edges if def-use edges are used for propagation. Why? Because def-use chains include def-use edges formed through non-executable edges. executable executable executable
11
Def-Use Edges vs SSA-edges
Algorithm based upon Def-Use chains will be less efficient. SSA-based algorithm: Time spent is proportional to number of SSA-edges. Number of SSA-edges per variable is O(N) where N is the number of statements. Therefore time complexity is O(NxV) where V is the number of variables. Def-Use based algorithm: There can be O(N) definitions and uses of one variable. Thus, number of def-use edges is O(N2) and hence the time complexity is O(N2xV).
12
Sample Problem For the given code segment perform conditional constant propagation and folding: (a) Using data flow based algorithm; and (b) Using SSA-form based algorithm. A = 1; W = 1; B = 2; if (A != B) then Z = B + A else Z = B – A endif do W = 2; if (Z < 0) then V = Z + 1 else V = Z - 1 endif; B = B – 1; while (B ≤ 0) Print V+W;
13
Another Application of SSA Form Global Value Numbering
14
Global Value Numbering
A technique for determining when two computations in a program are equivalent can be used for redundancy removal. Constant Propagation -- by computing values of two computations they can be shown to be equivalent. Common Subexpression Elimination -- lexically identical expressions can be shown to be equivalent without computing their values by observing that values of variables used have not changed. Value Numbering -- lexically different expressions can be shown to be equivalent without computing their values.
15
Examples Read A B A + 2 C A D C + 2 Show B==D VN succeeds
CP fails CSE fails A 5 B A + 2 C A + 1 D C + 1 Show B==D CP succeeds VN fails CSE fails
16
Examples If (..) then read X; A X + 1 --1 else read X; B X + 1 --2
C X VN fails – it concludes all three evaluations of X+1 use different values of X CP fails CSE succeeds If (..) then read X; T A X + 1 else read X; T B X + 1 C T
17
Value Numbering Algorithm
Congruence of variables Variables partitioned into congruence classes All variables in a congruence class have the same value Application of identical operators to equivalent arguments produces equivalent results Why SSA? To distinguish values of a variable at different program points we can use different names for them. Renaming is already performed during construction of SSA form.
18
Treatment of ϕ Nodes If P then A1 5 else A2 3 A3 ϕ (A1, A2)
If Q then B1 5 else B2 3 B3 ϕ (B1, B2) Is A3== B3? No -- P and Q may evaluate differently. Treat ϕ ‘s at different join points as different operators. A3 ϕP (A1, A2) B3 ϕQ (B1, B2)
19
Congruence Partitioning Algorithm
Pessimistic – underestimate partition sizes and merge partitions as equivalences are discovered. Optimistic – overestimate partition sizes and split them as contradictions are discovered. P\iQ – set of variables in P whose ith operand is in Q P/iQ – set of variables in P whose ith operand is not in Q Q properly splits P for i if P\iQ =/= {} and P/iQ =/= {}
20
Optimistic Algorithm Worklist {} for each operator f do Cf {}
for each X f(A,B) do Cf Cf U {X} Worklist Worklist U {Cf} While Worklist =/= {} do delete D from Worklist and use it to split all other classes for each class C properly split by D do if C ε Worklist then Worklist Worklist – {C} U {C\iD, C/iD} else Worklist Worklist U {C\iD} or Worklist Worklist U {C/iD}
21
Example A1input B1A1+1 C1A1 D1C1+1 E1B1+1 F1D1+1 E2D1+1 F2B1+1
Cinput = {A1,C1} C+ = {B1,D1,E1,F1,E2,F2} Cϕ = {E3,F3} Worklist = {Cinput, C+, Cϕ} Remove Cinput & use it to split C+ & Cϕ C+ \1 Cinput = {B1,D1} C+ /1 Cinput = {E1,F1,E2,F2} Worklist = {C+ \1 Cinput, C+ /1 Cinput , Cϕ} Remove C+ \1 Cinput ……. No splitting occurs Remove C+ /1 Cinput ……. No splitting occurs Remove Cϕ ……… No splitting occurs A1=C1 B1=D1 E1=F1=E2=F2 E3=F3 A1input B1A1+1 C1A1 D1C1+1 E1B1+1 F1D1+1 E2D1+1 F2B1+1 E3ϕ(E1,E2) F3ϕ(F1,F2)
22
Example A1input B1A1+1 C1A1 D1C1+1 A1input B1A1+1 E1B1+1
B1=D1 E1=F1=E2=F2 E3=F3 E1B1+1 F1D1+1 E2D1+1 F2B1+1 E1B1+1 E2B1+1 E3ϕ(E1,E2) E3ϕ(E1,E2) F3ϕ(F1,F2)
23
Sample Problem input A; input B; X = A + B; Y = B + A; while (…) do
Z = 1; P = Z + 1; W = 2; R = Z + 1; if (…) then W = A + 1; Q = Z + 1 else V = A + Z endif; A = A + 1; B = B + 1; X = A + 1; Y = B + 1; endwhile For the given code segment perform global value numbering to identify congruence classes of variables.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.