110 likes | 210 Views
CS 201 Compiler Construction. Lecture 10 SSA-Based Sparse Conditional Constant Propagation. Conditional Constant Propagation. SSA-based algorithm is faster than the previous algorithm that was presented earlier.
E N D
CS 201Compiler Construction Lecture 10 SSA-Based Sparse Conditional Constant Propagation
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.
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
Contd.. When can we process a 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 ecah 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.
Contd.. Maintain two worklists: FlowWorkList (for control flow edges) SSAWorkList (for SSA edges) • Initialize: FlowWorkList Edges leaving the entry node. SSAWorkList {} ExecutableFlag(FlowEdge)False Lattice cells initialized to UNDEF or top 2. Halt execution when both worklists are empty. Execution may proceed by processing edges from either worklist.
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 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
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.
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 or top. Compute the lattice value of the variable on the left hand side.
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 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.
Def-Use Edges vs SSA-edges executable executable executable 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.
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).