In computer science, sparse conditional constant propagation (SCCP) is an optimization frequently applied in compilers after conversion to static single assignment form (SSA). It propagates constants, which is the calculation of static values which can be calculated at compile time. Moreover, it can find more constant values, and thus more opportunities for improvement, than separately applying dead code elimination and constant propagation in any order or any number of repetitions. The algorithm operates by performing abstract interpretation of the code in SSA form. During abstract interpretation, it typically uses a flat lattice of constants for values and a global environment mapping SSA variables to values in this lattice. The crux of the algorithm comes in how it handles the interpretation of branch instructions. When encountered, the condition for a branch is evaluated as best possible given the precision of the abstract values bound to variables in the condition. It may be the case that the values are perfectly precise (neither top nor bottom) and hence, abstract execution can decide in which direction to branch. If the values are not constant, or a variable in the condition is undefined, then both branch directions must be taken to remain conservative. Upon completion of the abstract interpretation, instructions which were never reached are marked as dead code. SSA variables found to have constant values may then be inlined at (propagated to) their point of use.
Algorithm The algorithm originally described by Wegman and Zadeck assumes a program in SSA form consisting of three types of nodes.
Assignment nodes - these are instructions which use one or more values to compute and assign to a variable. These include phi expressions Conditional nodes - these are branches in the flow of the program conditional on the value of a variable or expression. It is assumed conditional nodes have no side effects. Root node - this is the entry point into the program. The output of the algorithm is a mapping from variables in the program to lattice values describing compile time knowledge of the values of the variables. The SCCP lattice consists of three levels.
Undefined - representing values for which nothing is known. Constant - representing values which have a known constant value at compile time. Overdefined/variable - representing values which cannot be proven to be constant at compile time. The meet function of this lattice is defined with Undefined as the top of the lattice and overdefined as the bottom. The algorithm initially marks inputs to the program as overdefined and all other variables as undefined, and marks all edges of the control flow graph (CFG) as unreachable. It maintains two worklists which it uses to iterate the CFG.
Flow work list - contains edges of the CFG which have been identified as reachable SSA work list - contains nodes of the CFG The flow work list is initialized with the edges of the CFG exiting the root node. The SSA work list is initially empty. Each step of the algorithm takes an item from either worklist. The algorithm will terminate regardless of the order items are taken. If the item is an edge from the flow work list
If the edge is marked as reachable, skip this edge and proceed to the next item in the flow work list or ssa worklist. Otherwise mark the edge as reachable. For each phi at the destination node mark the variable which the phi assigns to as the meet of all operands of the phi for which the corresponding incoming edge is marked reachable (ie. operands corresponding to unreachable edges are treated as undefined). If the phis lattice value changes, add all nodes which use the variable assigned by the phi to the SSA worklist If only one of the incoming edges of the destination node is marked reachable (ie. this is the first time the destination node is being evaluated): If the node is an assignment node evaluate the lattice value of the assigned variable based on the current lattice values of the variables on which it depends. If it depends only on constants, it is constant. If it depends on any undefined variables it is undefined. If it depends on constants and overdefined variables it is overdefined. Certain special cases may be taken into consideration (ie. any value multiplied by 0 is constant). If the lattice value of the assigned variable is changed then add all assignment nodes in the CFG which refer to the assigned variable to the SSA work list. Additionally if the lattice value of the assigned variable is changed, then for every conditional node which refers to the assigned value evaluate the condition expression lattice value. If the condition expression lattice value is overdefined, add all outgoing edges to the flow work list. If the value is constant, determine the edge executed as the result of the branch and add only that edge to the flow work list. If the value is undefined, do nothing. If the destination node contains only one outgoing edge (ie. it is not a conditional node) add the outgoing edge to the flow worklist. If the item is a node from the SSA work list
… excerpt ends here. Continue reading the full article.