0% found this document useful (0 votes)
11 views

Week 5

The document discusses control dependence graphs and program dependence graphs for representing control and data dependencies in programs. It defines key concepts like dominators, control dependence, and program slicing. Control dependence graphs represent control dependencies between program statements based on control flow. Program dependence graphs combine control dependence graphs and data dependence graphs to represent both control and data dependencies in a single graph. Program slicing is the technique of extracting a subset of a program that affects the values at a particular program location, based on reaching definitions in the program dependence graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Week 5

The document discusses control dependence graphs and program dependence graphs for representing control and data dependencies in programs. It defines key concepts like dominators, control dependence, and program slicing. Control dependence graphs represent control dependencies between program statements based on control flow. Program dependence graphs combine control dependence graphs and data dependence graphs to represent both control and data dependencies in a single graph. Program slicing is the technique of extracting a subset of a program that affects the values at a particular program location, based on reaching definitions in the program dependence graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Software Re-engineering

Week-5
A Graph-Based Model
Control Dependence Graph
A Control Dependence Representation
A Control Dependence Representation
A Control Dependence Representation
A Graph Representation of Behavior
Calculating Control Dependencies
Calculating Control Dependencies
Calculating Control Dependencies
Definition of Dominance
Dominator
• Pre-dominators in a rooted, directed graph can be used to make this intuitive
notion of “controlling decision” precise.

• Node M dominates node N, if every path from the root to N passes through M.
A node will typically have many dominators, but except for the root, there is
a unique immediate dominator of node N which is closest to N on any
path from the root, and which is in turn dominated by all the other
dominators of N.

Because each node (except the root) has a unique immediate dominator, the
immediate dominator relation forms a tree.

• Post-dominators are calculated in the reverse of the control flow graph, using a
special “exit” node as the root.
An Example of Dominators
Example
Every path starting from the root node to node
10 reaches through nodes 2, 4, 5, and 1. Thus,
these nodes are pre-dominators of node 10.

However, the immediate pre-dominator of 10 is


5.

All paths from 4 to the exit node passes


through 5 and 10; thus, these are the post-
dominators of 4.
However, the immediate post-dominator of 4 is
five and of 5 is 10.
Forward Dominance Tree
Forward Dominance
Forward Dominance (a.k.a. post dominance,
inverse dominance
A Graph-Based Definition
An Example of Control Dependence
A Graph-Based Model
Program dependence Graph
• Program Dependence

• To understand the dependence relationship in a program, it is best to first


examine it from a program execution trace

• A program execution trace is the complete sequence of instructions executed


under a specific input, associated with the memory locations and registers
visited by each instruction

– An instruction may appear multiple times in the trace


– We differentiate different instances of the same instruction by a sequence
number
Program dependence Graph
• PDGs are directed graphs with program statements as nodes and 2
types of edges:

1. One for representing control dependency


2. Another for data dependencies.

It essentially combines CDG and DDG in a single graph.


Data dependences and control dependences
• If instruction A defines a value used by instruction B (i.e. def(A) reaches
use(B)), we say B has a flow dependence on A, or in short B has a
dependence on A. Or B is dependent on A.

• Such reaching definitions define a fundamental dependence relationship


among program operations
– It limits the freedom with which the operations may be reordered
(or executed in parallel) for efficiency
– It can guide software fault localization
• Flow dependences are transitive:
– B is dependent on A, C is dependent on B→C is dependent on A
– We say there is a def/use chain from A to C
Program Dependence Graph
Program Dependence Graph
Create PDG
Solution
Program Slicing – Introduction
• Extract an executable subset of a program that (potentially) affects
the values at a particular program location.

• Slicing criterion: program location + variable

• Observer focusing on the slicing criterion cannot distinguish the run


of the program from a run of the slice
Program Slicing – Introduction
Program Slice definition:

• A slice is taken with respect to a slicing criterion <s,v>, which specifies


a location (statement s) and a variable (v).

• For statement s and variable v, the slice of program P with respect to


the slicing criterion <s,v> includes only those statements of P needed
to capture the behavior of v at s.
Program Slicing – Introduction
Applications of program slicing:
- understanding: which statements influence this statement
- debugging: focus on parts of program relevant for a bug
- testing
- parallelization: determine parts of program that can be executed
independently of each other.
- integration
- software quality
- software maintenance
- software metrics
Example
1. var n = readInput();
2. var i =1;
3. var sum= 0;
4. var prod=1;
5. while (i<=n){
6. sum=sum + i;
7. prod= prod*i;
8. i= i+1; }
9. print(sum);
10. print(prod);
Slicing
• Static vs. Dynamic

Static Slice: because they are computed as the solution to a static analysis problem
(without considering the program’s input)

Dynamic Slice: A slice is computed for a particular fixed input.

• Forward vs. Backward

Backward Slice: statements that influence the slicing criterion


(which statements make us have this particular value at this particular location in the code)

Forward Slice: statements that are influenced by the slicing criterion


(What statements are affected by the value of v at statement s?)
Static Program Slicing
• Graph reachability problem based on Program Dependence Graph
(PDG)

• PDG: a directed graph representing the data and the control


dependencies between statements

• Nodes: statements, predicate expressions


• Edges: data flow dependences: def-use pair, control flow dependences
Def-Use Pair
Def\us 1 2 3 4 5 6 7 8 9 10 1. var n = readInput();
e
2. var i =1;
1 x 3. var sum= 0;
2 x x x x 4. var prod=1;
3 x x 5. while (i<=n){
4 x x 6. sum=sum + i;
5 7. prod= prod*i;
6 x x 8. i= i+1; }
7 x x 9. print(sum);
8 x x x x 10. print(prod);
9
10
Control Flow Dependences
• Post Dominator:
Node n2 post dominates node n1 if every path n1….exit in the CFG
contains n2

• Control Dependence:
Node n2 is control dependent on node n1 ≠ n2 if
• there exists a control flow path P= n1….n2 where n2 post dominates any node
in P (excluding n1), and
• N2 does not post dominate n1
Post dependences
n1\n2 1 2 3 4 5 6 7 8 9 10
1 x x x x x x
2 x x x x x
3 x x x x
4 x x x
5 x x
6 x x x x x
7 x x x x
8 x x x
9 x
10
Computing Slices
Given:
• PDG
• Slicing Criterion (n, V) where n is statement and V is a set of variables
defined or used at n

Slice for (n, V):


• All statements from which n is reachable
• (all statements on which n depends)
Example
Entry

1. var n = readInput();
2. var i =1; 1 2 4 10
3 5 9
3. var sum= 0;
4. var prod=1;
5. while (i<=n){
6. sum=sum + i;
7. prod= prod*i;
6 7 8
8. i= i+1;}
9. print(sum); Slice (9, {sum})
10. print(prod);
{n |reachable(n,9)}
{1,2,3,5,6,8,9}
Class Activity
1. var x=1;
2. var y=2;
3. If (x<y){
4. y=x;}
5. Var z= x;

Draw the PDG and compute slice(5,{x})


Data Dependences
1. True or Flow-Dependent
y is true-, or flow-dependent on x, x δf y, iff x defines a variable that is an
input variable to y, such that x’s result may be used by y. This is also called
a read after write (RAW) dependence.

2. Anti-Dependent
y is anti-dependent on x, x δa y iff y defines a variable that is an input
variable to x. This is also called a write after read (WAR) dependence.

3. Output-Dependent
y is output-dependent on x, x δo y iff y defines a variable that is an output
variable for x. This is also called a write after write (WAW) dependence.
Example

You might also like