Problemcore
Program slicing
Also called static slicing, backward slice.
Given a program point and a set of variables, find the statements that may affect their values there.
- Time
- O(n + e) dependence-slice
- Where
- e = the number of edges of the program dependence graph; n = the number of statements
- Model
- Word RAM
Program slicing is the problem of finding the statements of a program that may affect the values of some variables at some program point. The point and the variables form the slicing criterion, and the answer is a slice. Mark Weiser defined slices and the problem (ICSE 1981, IEEE TSE 1984) as a way to cut a program down for debugging. This page states the problem. Weiser's algorithm, slicing by dependence graph reachability, system dependence graph slicing and dynamic slicing solve versions of it.
In plain words
When a variable has a wrong value at some line, a programmer reads the code backward. Which lines wrote the variable? Which lines computed the values that those lines used? Which tests decided whether those lines ran at all? The lines found this way form a slice. The other lines can be hidden, because they cannot change the value being studied. A slice is often much smaller than the program. The smallest possible slice depends on what the program really computes, and no tool can find it for every program. So tools find a slice that can be too large. They follow every write and every test that could matter on some path, even when that path never runs.
Definition
Programs are in the toy IR of the control flow graph pages. A statement is an instruction or a terminator. Write for statement of block , where equals the number of instructions of for its terminator. A program point is the point just before a statement. Every block is assumed to reach a ret.
Criterion and observations. A slicing criterion is a pair of a point and a set of variables. For an input , let be the sequence of tuples of values that the variables of have each time the run of on reaches . The sequence is finite or infinite, and it ends early if the run traps.
Executable slices. Fix a set of instructions and branches of . The projection of on deletes every instruction outside . It replaces every branch br a L1 L2 outside , at the end of block , by jump d, where is the immediate post-dominator of , or by ret 0 when that post-dominator is the exit. It keeps every jump. A ret a keeps its operand only when is that return and ; otherwise it becomes ret 0, which changes no observation. The projection is a slice of for when, for every input on which the run of reaches a ret, the run of on also reaches a ret and
where and, for , is the point of before the first kept statement at or after position in block . The size of a slice is . = all instructions and branches always gives a slice, so slices exist. A statement-minimal slice is one of least size. This is Weiser's definition in the toy IR. Weiser's own programs had structured statements, and deleting a structured statement plays the role of the jump to the post-dominator.
Dependence slices. A statement is data dependent on a statement if defines a variable , reads , and some path in the control flow graph goes from just after to with no definition of before (data dependence, flow dependences only). A statement is control dependent on a branch as defined on the control dependence page. The dependence slice of is the least set of instructions and branches such that:
- holds every instruction that defines some such that some path goes from just after to with no definition of ;
- holds every branch on which the statement at is control dependent;
- if and is data or control dependent on an instruction or branch , then .
The dependence slice is what all practical tools compute. It is a set of nodes of the program dependence graph, found by backward reachability. Used as , it gives a slice in the executable sense for every program in which every block reaches a ret; the proof is on the Weiser's algorithm page. It is not always statement-minimal.
Forward slices. The forward slice of a statement is the set of statements that depend on it, directly or through a chain of dependences. It answers "what can this change affect", while the backward slice answers "what can affect this".
Variants
- Static or dynamic. A static slice must work for every input. A dynamic slice must work for one given input, and it is often much smaller. It may be wrong for every other input.
- Executable or closure slices. Weiser asks for a program that runs. A dependence graph slice is a set of nodes, and the jumps and the block structure must be rebuilt before it runs. For programs with unstructured jumps, plain deletion can give a wrong program, and slicers must add jumps back; the survey of Tip covers the fixes for unstructured control flow.
- Interprocedural. Across calls, a slice should follow only paths where each return goes back to the call that entered the procedure. System dependence graph slicing does this. A slicer that ignores calling context still gives a correct slice, but a larger one.
- Conditioned slicing. The criterion also gives a predicate on the inputs, and the slice must work only for the inputs that satisfy it. Static and dynamic slicing are the two ends: no condition, and a condition that fixes one input.
- Amorphous slicing. The slice may be any program that gives the same observations, not only a projection. It can be smaller, but it is no longer a subset of the original code.
- Chopping. A chop of a source statement and a target statement holds the statements on dependence chains from the source to the target. Within one procedure, it is the intersection of the forward slice of the source and the backward slice of the target.
Examples
Two slices of one loop. The program below returns , the sum of the odd numbers from 1 to , and also computes (with for ).
B1:
i = 1
s = 0
p = 1
jump B2
B2:
t = i <= n
br t B3 B6
B3:
p = p * i
u = i % 2
br u B4 B5
B4:
s = s + i
jump B5
B5:
i = i + 1
jump B2
B6:
ret s
For the criterion the dependence slice has 6 statements: i = 1, p = 1, t = i <= n, br t B3 B6, p = p * i and i = i + 1. The parity test and both statements on drop out. In the projection, br u B4 B5 becomes jump B5, because B5 is the immediate post-dominator of B3, and ret s becomes ret 0. For the criterion the slice has 8 statements: all but p = 1 and p = p * i. The two slices share the loop control: i = 1, t = i <= n, the branch on t and i = i + 1. On the inputs the pair at B6 is , and each slice gives the same value of its variable as the full program.
A non-example. Take = i = 1, p = 1, p = p * i, i = i + 1, without the loop test. Then br t B3 B6 becomes jump B6, the loop body never runs, and the projection gives at B6 for every . For the program gives 6, so this is not a slice. The control dependence of the loop body on the test is what forces the test into the slice.
A dependence slice that is not minimal.
B1:
y = 1
t = a * a
c = t < 0
br c B2 B3
B2:
y = 2
jump B3
B3:
ret y
For the dependence slice holds all 5 instructions and branches, since y = 2 may reach B3 and depends on the branch. But for every integer , so and B2 never runs. y = 1 is a slice of size 1, and it is statement-minimal, because with the variable is undefined at B3. To see this, a slicer must know that a square is never negative, which is a fact about values, not about the graph.
Known algorithms
| algorithm | slice | method | cost for one criterion |
|---|---|---|---|
| Weiser (1981, 1984) | static, executable | iterated backward dataflow equations for relevant variables, plus branches | set operations for branches and variables |
| dependence graph reachability (Ottenstein and Ottenstein 1984) | static, set of nodes | backward reachability in the program dependence graph | after the graph is built |
| system dependence graph (Horwitz, Reps and Binkley 1990) | static, interprocedural | summary edges, then a two-phase backward traversal | linear in the size of the graph with its summary edges, after they are built |
| dynamic slicing (Korel and Laski 1988; Agrawal and Horgan 1990) | one input | dependences seen in a trace of the run | linear in the size of the dependence graph of the trace |
Here is the number of statements, the number of control flow edges and the number of edges of the dependence graph.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Dynamic slicing | O(T n) worst | O((v + b) n) extra | deterministicnot executable resultonlineiterative | 1 language |
| Slicing by dependence graph reachability | O(n + e) worst | O(n) extra | deterministicnot executable resultofflineiterative | 1 language |
| System dependence graph slicing | O(N + E) worst | O(N + E) extra | context sensitivedeterministicnot executable resultofflineiterative | 1 language |
| Weiser's slicing algorithm | O((c + 1) ((n + m) v^2 + c n)) worst | O(n v + c n) extra | deterministicexecutable resultofflineiterative | 1 language |
Bounds
Minimal slices are not computable. Weiser showed that no algorithm finds a statement-minimal slice for every program and criterion. A short proof in the toy IR follows.
Proof. The toy IR has unbounded integers and the operators *, / and %, so a toy IR program can simulate any Turing machine on a fixed input : it keeps the two halves of the tape as two numbers in base and moves the head with multiplication, division and remainder. Build the program
B1:
x = 0
jump SIM
SIM:
# the blocks that simulate M on w; they never read or write x
...
DONE:
x = 1
ret x
where the simulation jumps to DONE when halts, and every block of the simulation has a path to DONE. Take the criterion , the point before ret x. If halts on , the program reaches that point once, with , and returns. Every slice must then keep x = 1, since without it is 0 or undefined there. The set x = 1 is a slice. In its projection every block other than DONE ends in a jump to its immediate post-dominator (a block with one successor has that successor as its immediate post-dominator), so every run climbs the post-dominator tree, reaches DONE, sets and returns. So the minimal size is 1. If does not halt on , the program never reaches a ret, the condition on slices is empty, and is a slice of size 0. So the minimal size is 1 if and only if halts on . An algorithm that returns a minimal slice would decide the halting problem, which is impossible.
The same argument, with "the slice keeps statement " as the property, shows that no algorithm decides whether a given statement belongs to some or to every minimal slice: in the program above, x = 1 is the only statement of the unique minimal slice when halts, and the unique minimal slice is empty when does not halt. The argument has the same shape as the proof of Rice's theorem.
Dependence slices are cheap. Given the program dependence graph, the dependence slice is one backward search, so it costs time. The graph itself can have edges, as the program dependence graph page shows, so building it dominates when only a few slices are needed.
Reductions
- From halting. The proof above reduces the halting problem to finding a minimal slice.
- To graph reachability. A dependence slice is the set of nodes from which the criterion node is reachable in the program dependence graph (graph reachability).
- To CFL reachability. With procedures, a precise slice is the set of nodes that reach the criterion by a path whose call and return edges match, which is a CFL reachability problem over a Dyck language of call sites (Reps 1998).
- Dead code. Aggressive dead code elimination keeps exactly the slice of all critical statements, such as returns, stores, calls and operations that may trap, and removes the rest.
Applications
- Debugging. Weiser's motivation: the statements outside the slice of a wrong value cannot be the cause. Dynamic slices narrow this to the statements that acted in the failing run.
- Program understanding and maintenance. A slice shows which code computes one output, and a forward slice shows what a change can affect.
- Testing. After a change, only the tests whose outputs have the changed statement in their slice need to run again.
- Optimization. Dead code removal and the extraction of independent computations for parallel execution both use slices of the dependence graph.
References
- Mark Weiser, Program slicing, Proceedings of the 5th International Conference on Software Engineering, San Diego, March 1981, pages 439-449, the slicing criterion, slices obtained by deleting statements, and the dataflow equations for slices
- Mark Weiser, Program slicing, IEEE Transactions on Software Engineering SE-10(4), 1984, pages 352-357, the definitions of criterion and slice, the argument that statement-minimal slices cannot be computed in general, and the iterative algorithm
- Frank Tip, A survey of program slicing techniques, Journal of Programming Languages 3(3), 1995, pages 121-189, the sections on static slicing (dataflow equations, dependence graphs, procedures, unstructured control flow) and on dynamic slicing
- Susan Horwitz, Thomas Reps and David Binkley, Interprocedural slicing using dependence graphs, ACM Transactions on Programming Languages and Systems 12(1), 1990, pages 26-60, slices as reachability in dependence graphs, and the problem of calling context for slices across procedures
- Thomas Reps, Program analysis via graph reachability, Information and Software Technology 40(11-12), 1998, pages 701-726, interprocedural slicing as a CFL-reachability problem
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 WP2EC3 Edit this page History Report an error Markdown