Docsity

Control Flow and Data Flow Analysis for a Given Program, Exams of Computer Science

Toggle sidebar
bg1
631 exam practice mid-term
This program is to be used in questions 1-3.
S0 if (z <= 1) goto L1
S1 x = 1
S2 if (z >2) goto L2
S3 y = x+1
S4 goto L3
S5 L1 x = 2
S6 L2 z = x – 3
S7 x = 4
S8 z = x+7
S9 L3 print x,y,z
1. Control flow analysis
(a) What are the basic blocks?
(b) Show the control flow graph
(c) Number the nodes of the CFG in reverse postorder.
(d) Give the dominator tree for this program
Answer:
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Control Flow and Data Flow Analysis for a Given Program and more Exams Computer Science in PDF only on Docsity!

631 exam practice mid-term This program is to be used in questions 1-3. S0 if (z <= 1) goto L S1 x = 1 S2 if (z > 2) goto L S3 y = x+ S4 goto L S5 L1 x = 2 S6 L2 z = x – 3 S7 x = 4 S8 z = x+ S9 L3 print x,y,z

  1. Control flow analysis

(a) What are the basic blocks? (b) Show the control flow graph (c) Number the nodes of the CFG in reverse postorder. (d) Give the dominator tree for this program

Answer:

  1. Control dependence - which block(s) are control-dependent on which block(s)?

Block is control dependent on edge B2 B1 → B B3 B1 → B B4 B3→B4 and B1 → B B5 B3 → B

  1. Dominance frontiers and SSA form

(a) For each block, give the dominance frontier and the iterated dominance frontier. Block DF DF+ B2 B4 B4,B B3 B4,B6 B4,B B4 B6 B B5 B6 B

(b) Give the (minimal) SSA form of the program.

if (z 0 <= 1) goto L x 1 = 1 if (z 0 > 2) goto L y 1 = x 1 + goto L L1 x 2 = 2 L2 x 4 = φ(x 1 , x 2 ) z 1 = x 4 – 3 x 3 = 4 z 2 = x 3 + L3 x 5 = φ(x 1 , x 3 ) y 2 = φ(y 1 , y 0 ) z 3 = φ(z 0 , z 2 ) print x 5 ,y 2 ,z 3

  1. Data flow lattices

(a) Remember that we define our v operator as u v v ≡ u = u u v. A frame work is monotone if and only if u v v ⇒ f (u) v f (v). Prove or disprove that even if a framework is not distributive (i.e., f (uuv) = f (u)uf (v)), monotonicity guarantees that f (u u v) v f (u) u f (v). Answer: First, we prove that uuv v v. To prove this, we need to prove uuv = uuv uv, which is true since u is associative v u v = v.

(a) What is your lattice (e.g., the type of the values you compute at each point)? For each variable, the value can be > (no definitions seen), null, non-null, or ⊥ (either null or non-null).

(b) Discuss the ways in which your analysis is conservative, how it generate incorrect (but conservative) answers, and why the decisions we might make on it are safe. We might compute an answer of ⊥ for a variable when the variable could only be null or could only be non-null. We could be approximate because of infeasible paths or because of data constraints we don’t understand (e.g., the next pointer might never be null). (c) What is your meet function? For each variable, the appropriate element-wise meet is shown below:

  • ⊥ ∧ X ≡ ⊥
  • X ∧ X ≡ X
  • ∧ X ≡ X

  • null ∧ non-null ≡ ⊥

(d) I’ll use a forward solution.

(e) Use > to initialize In values. (f) Describe how to compute the transfer functions. p = q Copy information for q to information for p. p = null Set p to null. p = &x Set p to non-null. p.f Set p to non-null. We could do something where if p were known to be null, we would set all values to top (it would be an infeasible path). But then the data flow problem would not be monotone and there would many questions about what answer we were computing. p = q.f Set p to be ⊥ and q to be nonnull. (g) If your framework monotone? Is it distributive? Justify your answers. Given the limitation on how we handle a read of p.x, it is monotone. If v ≤ u, then f (v) ≤ f (u). It fact, it is distributive as well f (u)∧f (v) = f (u∧v). The transfer function simple sets specific values for some variables, and copies values for other variables. Doing the meet before or after makes no difference.

(h) Briefly describe the changes that would need to be made to take into account conditional tests such as if p == null goto L5. We would have different transfer fuctions, one for each exit from a block. If a exit would be taken only under certain conditions, that would be reflected in the transfer function (in the above example, the transfer function for the exit that led to L5 would set p to null and the other exit would set p to non-null.

  1. Consider the following program...

(a) Give the control flow graph for this program.

B0 X := read() Z := read() if (...) goto B goto B2
/
/
/
B2 Y := XX B1 Y = X+X ^ write Y write Y | goto B3 goto B | \ / | \ / | B3 if (...) goto B | goto B | /
| /
B6 W := Z
Z B4 Y = X*X X := X+W write Y goto B2 if (...) goto B goto B /
/
B7 Y := X+X B5 write Y goto B5 -------------> goto Exit / / Exit (b) Give the dominator tree for this program. B0 +-- B +-- B +-- B3 +-- B +-- B4 +-- B5 --- Exit +-- B (c) Give the control dependence relation (which block is control dependence on which block)

for this program.

Block Controls B0 B1, B B3 B2, B3, B B4 B (d) Give the iterated dominance frontier for each block.

Answer OK, I’m going to use a simple lattice and not keep track of pointers to pointers, other than to just recognize that they are a pointer. The type NUMBER will indicate either an INT or a FLOAT. The lattice for each variable is shown below:

The meet function is simply taking the greatest lower bound for each variable. The data flow graph is initialized to a value of > for variable. Assume that uninitialized variables are assumed to have the integer value 0. Then the value at start is for all variables to have type I.

OK, now the difficult part: computing the transfer function. For expressions, the following table describes the type of each expression. The left column gives the minimal types that will produce the result type in the right column. The type of an expression is determined by the first row that matches. For example, I + F has type F. I assume that syntactic constraints prevent things such as taking the address of an R-value.

expression type expression type expression type I + I I *PI I &I PI N + F F *PF F &F PF F + N F *PN N &N PN ⊥ + ⊥ N *⊥ ⊥ &⊥ P

OK, now we deal with assignment. Assignment to a simple variable sets the type of the variable on the left-hand side to be the type on the right-hand side. Further, the types of any pointers that might be pointing to the variable on the left-hand side are updated. Since we don’t have points-to analysis, we will just use type information. For example, if x was previously an Int, and a float value is assigned to x, then the type of x becomes F, and the types of any variables that might have been pointing to x (i.e., those with types PI, PN, P or ⊥ get merged with PF.

When we assign through a pointer (e.g., *p = 42), we have to make similar updates. If p had previously been of type PF, then *p = 42 causes all variables of type F, N or ⊥ to be merged with I. The framework is monotone, but not distributive. For example, in the code fragement below, the system would only determine that z was of type numeric, while a meets-over-all-paths solution would determine that z was of type float.

if (...) { x = 1; y = 2.0; } else { x = 1.0; y = 2; } z = x+y

The framework is approximate because:

  • Because the framework is not distributive, we only compute a maximal fixed point solution, rather than a meets-over-all-paths solution.
  • The transfer functions are approximate, particularly with regards to pointers
  • The presense of infeasible paths

However, in all cases we compute an answer that is ≤ ”truth”, which allows us to safely use the answer. Alternative solutions You might consider a solution which kept track of types such as PPI, PPPF. If the lattice was set up so that the meet of PPI and PPPI was bottom, then you don’t have any infinite ascending or decending chains, and everything works just fine. If instead the meet of PPI and PPPI is a type PP that indicates a pointer to a pointer to something, then you have to worry about infinite chains and non-termination.

  1. Exercise 1.3 from the handout

Answer: We know that for all X and Y

X ⊆ γ(Y ) ⇔ α(X) ⊆ Y ⇔ X ⊆ γ′(Y )

which directly reduces to X ⊆ γ(Y ) ⇔ X ⊆ γ′(Y ) which tells us that γ = γ′.

  1. Exercise 1.4 from the handout

Answer: This turned out to be too much work for an exam question. You need to do lots of steps, such as the one for Gexit(4) and Fexit(4) = RDexit(4) given in the middle of page 16. For reaching definitions you have F = α◦G◦γ. This would not be the case for other problems, in particular problems that are not distributive. The inductive proof is straightforward for the case where F = α ◦ G ◦ γ.