Problemcore
Constant propagation
Also called constant propagation analysis.
For each program point, find the variables that hold the same known value on every path, using a flat lattice that is monotone but not distributive.
Constant propagation asks, for each point of a program, which variables hold one fixed value on every path that reaches the point. A compiler uses the answer to replace reads by constants and to fold the arithmetic that follows, as in constant folding, and to remove branches whose direction is known. It is the standard example of a monotone dataflow framework that is not distributive. Its iterative solution is safe but can be strictly weaker than the meet over all paths, and that ideal answer cannot be computed at all. The strongest practical method is sparse conditional constant propagation.
In plain words
Walk through the program and keep a note for each variable: "not seen yet", "always this number", or "could be many things". An assignment of a number sets the note to that number. An addition of two known numbers gives a known number. Anything read from outside, such as a parameter, is "many things".
Where two paths meet, compare the notes. If both say the same number, keep it. If they differ, the variable is "many things". A loop sends notes back to its top, so you repeat the walk until no note changes.
The notes only move from "not seen yet" to a number and then to "many things", so the walk ends soon. The answer is never wrong, but it can miss a constant when the paths agree on a result while they disagree on the inputs.
Definition
Input. A control flow graph of three-address code with blocks, edges, instructions and variables, and one entry block. The parameters come from outside.
Output. For every block and variable , a value in the flat lattice below, and the same at the end of the block. A variable is a constant at a point if its value there is an integer .
The value lattice. For one variable, the flat lattice has ("no value yet", or not defined), every integer, and ("not a constant"), with above every integer and every integer above . The meet is , , and for . Two different integers are not comparable.
The framework.
| property | value |
|---|---|
| direction | forward |
| may or must | must: a constant must hold the same value on every path |
| domain | the maps from the variables to the flat lattice |
| order | pointwise; a lower value is a weaker claim |
| meet | pointwise meet of the flat lattice |
| and | every variable ; every variable |
| boundary value | at the start of the entry, the parameters are and the other variables are (not yet defined) |
| start value elsewhere | |
| transfer functions | the rules below, monotone and not distributive |
| height | per point, since each variable can fall at most twice |
The page uses the meet convention of Kildall. Turning the order upside down gives the join convention of abstract interpretation, where is "no value yet".
Transfer functions. Let be the map before an instruction.
x = ksets to .x = ysets to .x = a op bsets to the value of the operation on and , where an integer operand stands for itself. If either operand is , the result is . Otherwise, if either operand is , the result is . Otherwise both are integers, and the result is the folded value, except that an operation that would trap, a division or remainder by 0 or a shift by a count outside 0 to 63, gives . A trapping operation is never folded.
A block applies its instructions in order. Each rule is monotone: if the operands go down, the result goes down. A operand gives , which is above everything; an operand that is not stays not when it goes down, and the result then moves only from the folded value to .
Equations.
Meet over all paths. For a path from the entry to the start of , gives each variable the value it has after the instructions along , if it depends on a parameter or on a trap, and if does not define it. The meet-over-all-paths solution makes a constant at when every path that defines gives it . This is the ideal answer for the CFG, with every branch taken as possible.
The fixed point computed. The worklist dataflow algorithm starts every map at , puts at the entry, and lowers the maps until the equations hold. The result is the maximal fixed-point solution.
Soundness. Say that a map describes a state of a run if every variable that the run has defined has or equal to its value, and every variable with is undefined. The boundary value describes every start state. Each rule keeps the relation: an operand that is is undefined, so a run that reads it stops, and otherwise the folded value is the value that the run computes. By induction along the path of a run, describes the state at the end of . The MOP value is below every path value, and the MOP-MFP coincidence theorem gives for every monotone framework. A map below a map that describes a state also describes it, so MOP and MFP describe every run: if MFP says at a point, every run that reaches the point with defined has there.
Constant propagation is not distributive. Proposition. The function of z = x + y is not distributive. Proof. Let have and have . Then and both have , so has . But has , so has . As , . The same branches before z = x + y make MFP give while MOP gives ; the distributive dataflow framework page shows this case in a table. The gap is not only for two variable operands. For x = y * 0, the maps with and give each, but their meet has , and the rule gives . On the other hand, the rules for x = k, x = y and x = y + k are distributive, because the map is one to one.
Cost. Each map can fall at most times, so the worklist algorithm evaluates each block at most times. One evaluation of costs per predecessor for the meets, plus to copy the map, plus one step per instruction. The total is time and space.
Variants
- Simple constant propagation. The problem above, solved by the iterative algorithm of Kildall. Every branch is taken as possible.
- Conditional constant propagation. A branch whose condition is a known constant takes only one edge, and the blocks that no executable edge reaches are skipped. This finds more constants, because values from dead code no longer reach the meets. Sparse conditional constant propagation is the standard algorithm.
- Sparse constant propagation. On SSA form, each name has one definition, so one value per name is enough. Values flow along the def-use edges of the SSA graph instead of through every block.
- Copy-constant propagation. Only
x = kandx = yare evaluated, and every other assignment gives . This framework is distributive, so its MFP equals its MOP. - Linear constant propagation. Assignments of the form
x = a * y + bwith constants and are also tracked. It is distributive in the form of the IDE framework, which solves it across procedures with summaries. - Interprocedural constant propagation. Constants are passed into calls and back. See interprocedural constant propagation and the interprocedural dataflow analysis page.
- Richer domains. Ranges of values, as in the interval domain, or relations between variables, carry more than one value per variable. They need a widening operator, since their lattices have infinite height.
Examples
A loop. n is a parameter.
entry:
k = 5
i = 0
jump head
head:
t = i < n
br t body exit
body:
j = k * 2
i = i + 1
jump head
exit:
r = k + i
ret r
Round robin in the order entry, head, body, exit gives, at the start of head:
| pass | k | i | j | n |
|---|---|---|---|---|
| 1 | 5 | 0 | ||
| 2 | 5 | 10 | ||
| 3 | 5 | 10 |
In pass 2 the back edge brings , and . So k is the constant 5 everywhere, j is 10, and r = k + i is not a constant. The value at head and at exit uses : on the path that skips the loop, j is never defined, so no run reads another value of it there.
A known branch. No parameters.
entry:
x = 1
c = x == 1
br c then else
then:
y = 2
jump join
else:
y = 3
jump join
join:
z = y + 1
ret z
Simple constant propagation finds but takes both edges, so and at join. Conditional constant propagation sees that c is 1, marks only the edge to then as executable, and finds and .
Opposite branches. Branches that set to and to before z = x + y give in MFP, while every path gives . Neither simple nor conditional constant propagation finds this constant, since both meet the values of x and y before the addition.
Known algorithms
| algorithm | time | result |
|---|---|---|
| Worklist dataflow algorithm on the CFG | MFP of simple constant propagation; Kildall 1973 | |
| Sparse propagation on SSA form | linear in the size of the SSA graph | one value per SSA name; each value falls at most twice |
| Sparse conditional constant propagation | linear in the size of the SSA graph | also tracks executable edges; at least as strong as any order of simple propagation and branch removal |
| Path enumeration on an acyclic CFG | exponential in | MOP; only for small acyclic graphs |
Elimination dataflow methods do not apply in their usual form, because they need distributive functions with small representations.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| IDE framework | O(E D^3) worst · average | O(N D^2 + C D^2) extra | deterministiciterative | 1 language |
| Interprocedural constant propagation | O((v + 1)(n + b v) + (k + 1) n) worst | O(n + b v) extra | deterministicofflineiterative | 1 language |
| Sparse conditional constant propagation | O(n + m + s d) worst · average | Θ(n + m + s) extra | deterministiciterative | 1 language |
| Worklist dataflow algorithm | O((n + m)(h + 1)) worst · average | Θ(n) extra | deterministiciterative | 1 language |
Bounds
MOP cannot be computed. Proposition. No algorithm decides, for every program, whether a variable is the constant 0 at a point in the meet-over-all-paths solution. Proof. Reduce from Hilbert's tenth problem: no algorithm decides whether a polynomial with integer coefficients has a root in the natural numbers. Build a program that sets each to 0, then runs one loop per variable whose body is xi = xi + 1 and whose test is a parameter. After the loops it computes with +, - and *, then z = q == 0. The CFG paths through the loops realize every tuple of natural numbers, and on the path for the value of z is 1 if and 0 otherwise. So the MOP value of z at the end is the constant 0 exactly when has no root in the natural numbers. This agrees with the general result of Kam and Ullman that no algorithm computes the MOP solution of every monotone framework. If branch conditions are taken into account, deciding whether a variable is constant on all real runs is undecidable for an even simpler reason, by Rice's theorem.
MFP is cheap. The bound above is polynomial, and the sparse algorithms are linear in the size of the SSA form.
MFP can be strictly weaker than MOP. The opposite branches example has and . By the tightness part of the MOP-MFP coincidence theorem, every framework that is not distributive has such an instance.
Iteration speed. Constant propagation is not rapid. Round robin can need more passes than the of the Kam-Ullman iteration bound, which gives a loop with that needs 6 passes. The worklist bound evaluations per block always holds.
Reductions
- To a monotone framework. Constant propagation is an instance of a monotone dataflow framework of height per point, so every general solver applies.
- To sparse propagation on SSA. After SSA construction, the map at each point is replaced by one value per SSA name. A phi-function takes the meet of its operands. The values flow along def-use chains, so a change is sent only to the instructions that read the changed name.
- Linear constants to IDE. Linear constant propagation is an instance of the IDE framework, where the values are environments and the transfer functions are compositions of .
- From Hilbert's tenth problem. The reduction in Bounds shows that the MOP version is undecidable.
Applications
- Folding and simplification. Replace each read of a constant by its value and fold the instruction, as in constant folding. This often makes copy propagation and strength reduction possible.
- Branch folding and dead code. A branch on a known condition becomes a jump, and the blocks that are no longer reachable are removed by dead code elimination.
- Specialization. Interprocedural constant propagation finds parameters that are constant at every call, which function inlining and partial evaluation exploit.
- Value numbering. Global value numbering finds expressions with equal values, and it gains from the constants found first.
References
- Gary A. Kildall, A unified approach to global program optimization, Proceedings of the 1st ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages (POPL 1973), 194-206, the whole paper (constant propagation as an instance of the lattice framework, solved by the iterative algorithm)
- John B. Kam and Jeffrey D. Ullman, Monotone data flow analysis frameworks, Acta Informatica 7(3), 1977, 305-317, the whole paper (constant propagation as a monotone framework that is not distributive, and no algorithm for the meet over all paths in general)
- Mark N. Wegman and F. Kenneth Zadeck, Constant propagation with conditional branches, ACM Transactions on Programming Languages and Systems 13(2), 1991, 181-210, the whole paper (simple, sparse simple, conditional and sparse conditional constant propagation, compared on the same lattice)
- Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Pearson Addison-Wesley, 2007, Section 9.4 (Constant Propagation, the semilattice of values, the transfer functions, 9.4.4 monotonicity and 9.4.5 nondistributivity)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag CY112C Edit this page History Report an error Markdown