soma@ist.tugraz.at 1
Outline Basics Control flow graphs Slicing Motivation Static slicing with relevant variables table Static slicing with program dependency graphs Summary & Outline 2
Outline Basics Control flow graphs Slicing Motivation Static slicing with relevant variables table Static slicing with program dependency graphs Summary & Outline 3
Outline Basics Control flow graphs Slicing Motivation Static slicing with relevant variables table Static slicing with program dependency graphs Summary & Outline 4
Motivation Imagine: Your program computes the wrong value for variable x at line 312 in the file y. The statement in line 312 is correct. How can you minimize the statements you have to investigate? Solution: Computation of statements influencing the values of the variables referenced in line 312 and skip the rest. 5
What is a Slice? A Slice is a (reduced) program, that preserves the original program s behavior for a given set of variables at a chosen point in a program Characteristics: Defined for variables and a given point A slice is itself a program Ignoring other (irrelevant) statements Focus on relevant parts 6
Example 1 1. begin 2. z = 4 + a; 3. y = z + 1; 4. x = 5 + z; 5. end Slicing Criterion: (5, {x}) 7
Algorithm 1 Slicing criterion C = (i,v) with line number i, variables of interest V Relevant variables R C (n) all v V if n = I all v Ref(n) if { w w Def(n) w R C (m) n Pre(m)} all w R C (m) where w Def(n) n Pre(m) 1 2a 2b Slice S C comprises all statements n where R C (m) Def(n) { } with n Pre(m) 8
9 Example 2 1. begin 2. z = 4 + a; 3. if (a > 3) then 4. begin 5. z = z - 3; 6. end 7. fi; 8. x = 5 + z; 9. end Slicing Criterion: (9, {x})
Algorithm 2 Influence set Infl(b) Set of statements influenced by the control statement b Improved algorithm: 1. Compute R 0 C and S 0 C 2. Compute control flow stmts infl. variables in the slice: B C = { b j S 0 C, j Infl(b) } 3. Compute the slice S 1 C S 1 C = S 0 C B C 10
Example 3 1. begin 2. r = 0 ; 3. i = 0 ; 4. while ( i < n) do 5. begin 6. if (A[ i ] == 1) then 7. begin 8. r = r + 1 ; 9. end; 10. f i ; 11. i = i + 1 ; 12. end; 13. od; 14. end; Slicing Criterion: (14, {i}) 11
Example 4 1. begin 2. r = 0 ; 3. i = 0 ; 4. while ( i < n) do 5. begin 6. if (A[ i ] == 1) then 7. begin 8. r = r + 1 ; 9. end; 10. f i ; 11. i = i + 1 ; 12. n = n 1; 13. end; 14. od; 15. end; Slicing Criterion: (15, {i}) 12
Algorithm 3 1.Compute R 0 C and S 0 C 2.For i 0 a. Compute control flow statements influencing variables in the slice: B i C = { b j S i C, j Infl(b) } b. Compute statements which influence variables referenced in B i C: R i+1 C(n) = R i C(n) R 0 (b,ref(b))(n) c. Compute the slice S i+1 C S i+1 C = S i C B i C 13
Example 5 1. begin 2. a = 0 ; 3. i = 0 ; 4. while ( i < n) do 5. begin 6. a = b ; 7. b = i ; 8. i = i + 1 ; 9. end; 10. od; 11.end; Slicing Criterion: (11, {a}) 14
Program dependency graph Directed graph of a single procedure in a program Statements are the nodes Data and control dependencies are the edges Control dependency Statements in the branches of if or while statements are control dependent on the control predicate Data dependency Node j is data dependent on node i if x { x Def(i), x Ref(j) } and there exists a path P from i to j without redefining x. 15
Minimal slice Definition: Let C be a slicing criterion on a program P. A slice P of P on C is statement-minimal if no other slice of P on C has fewer statements than S. Theorem: There does not exist an algorithm to find statement-minimal slices for arbitrary programs. (Problem equivalent to halting problem) 16
17 Example 6 1. begin 2. A = constant; 3. while (K < 10) do 4. begin 5. if (C = 0) then 6. begin 7. B = A; 8. X = 1; 9. end; 10. else 11. begin 12. C = B; 13. Y = 2; 14. end; 15. fi; 16. K = K + 1 ; 17. end; 18. od; 19. Z = X + Y; 20. wr i t e (Z) 21. end; Slicing Criterion: (20, {z})
Outline Basics Control flow graphs Slicing Motivation Static slicing with relevant variables table Static slicing with program dependency graphs Summary & Outline 18
Types of slices A slice S can be obtained from a program P by deleting zero or more statements from P. S has the same values for the variables in V as P any input I (static slicing) for a given input I (dynamic slicing) 19
Slicing Criterion Static slice C = (i, V) with a line number i and a set of variables of interest V Example: C = (5, {x}) Dynamic slice C = (x, I q, V) with program input x, q th element of the trajectory I q and a set of variables of interest V Example: C = ({a = 4}, 5³, {x}) 20
Types of slices Backward Computation of statements influencing the value of a variable Forward Computation of statements influenced by a statement 21
Types of slices Input constraint Static slice Relevant variables table Program dependency graphs Dynamic slice Relevant slicing Direction Backward slice Forward slice 22
Open Problems Function calls call-by-value call-by-reference Other control flow statements goto break continue Composite data types arrays Pointer Interprocess communication 23
Questions? 24