CD Practice Question of Data Flow Analysis
CD Practice Question of Data Flow Analysis
For next three questions, consider the following three address code:
1: a = b * 2;
L1: 2: if (a >= c) goto L4;
3: d = 3*a + 4;
4: if (d <= c) goto L2;
5: x = 2 * c;
6: y = 7;
7: goto L3;
L2: 8: y = 3 * c;
L3: 9: a = a + 2;
10: goto L1;
L4: 11:
Q1. How many basics blocks are there in this code? ______
(Don’t count entry and exit block)
Q4. Which of the following is correct gen and kill sets of basics block B1 for Available
expression analysis?
(a) Gen = {a*b}, Kill = {} (b) Gen = {c+d}, Kill = {c+d}
(c) Gen = {a*b, c+d}, Kill = {} (d) Gen = {a+b, c+d}, Kill = {a*b}
Q5. Which of the following is correct gen and kill sets of basics block B2 for Available
expression analysis?
(a) Gen = {c+d, a*b}, Kill = {a*b} (b) Gen = {a*b, c+d}, Kill = {c+d, a*b }
(c) Gen = {c+d}, Kill = {a*b} (d) Gen = {c+d, a*b}, Kill = {a*b}
Q6. Which of the following is correct gen and kill sets of basics block B3 for Available
expression analysis?
(a) Gen = {a*b}, Kill = {c+d} (b) Gen = {a*b}, Kill = {a*b}
(c) Gen = {a*b, c+d}, Kill = {a*b} (d) Gen = {a*b}, Kill = {}
Q7. Which of the following is correct gen and kill sets of basics block B4 for Available
expression analysis?
(a) Gen = {a*b}, Kill = {a*b} (b) Gen = {c+d, a*b}, Kill = {a*b}
(c) Gen = {a*b}, Kill = {c+d, a*b} (d) Gen = {a*b}, Kill = {c+d}
Q8. Which of the following is correct in and out sets of basics block B1 for Available
expression analysis?
(a) In = {}, Out = {a*b, c+d} (b) In = {a*b}, Out = {a*b, c+d}
(c) In = {a*b, c+d}, Out = { a*b} (d) In = {a*b}, Out = { a*b, c+d }
Q9. Which of the following is correct in and out sets of basics block B2 for Available
expression analysis?
(a) In = {c+d, a*b}, Out = {a*b, c+d} (b) In = {a*b}, Out = {c+d}
(c) In = {a*b, c+d}, Out = {c+d} (d) In = {a*b}, Out = {a*b, c+d}
Q10. Which of the following is correct in and out sets of basics block B3 for Available
expression analysis?
(a) In = {c+d}, Out = {a*b, c+d} (b) In = {c+d}, Out = {c+d}
(c) In = {a*b, c+d}, Out = {a*b, c+d} (d) In = {a*b}, Out = {a*b}
Q11. Which of the following is correct in and out sets of basics block B4 for Available
expression analysis?
(a) In = {c+d}, Out = {} (b) In = {a*b}, Out = {c+d}
(c) In = {c+d}, Out = {a*b} (d) In = {a*b, c+d}, Out = {a*b, c+d}
For next Eight questions: Consider the following CFG
Q12. Which of the following is correct gen and kill sets of basics block B1 for reaching
definition analysis?
(a) Gen = {d1,d2,d3}, Kill = {d4,d5}
(b) Gen = {d1,d2,d3}, Kill = {d4,d5,d6,d7}
(c) Gen = {d1,d2}, Kill = {d4,d5,d6,d7}
(d) Gen = {d1,d2,d3}, Kill = {d4,d5,d6}
Q13. Which of the following is correct gen and kill sets of basics block B2 for reaching
definition analysis?
(a) Gen = {d4,d5}, Kill = {d1,d7}
(b) Gen = {d4,d5}, Kill = {d1,d2}
(c) Gen = {d4,d5}, Kill = {d1,d5,d6,d7}
(d) Gen = {d4,d5}, Kill = {d1,d2,d7}
Q14. Which of the following is correct gen and kill sets of basics block B3 for reaching
definition analysis?
(a) Gen = {d6}, Kill = {d3} (b) Gen = {d6}, Kill = {d3,d7}
(c) Gen = {d6, d3}, Kill = {d3} (d) Gen = {d6}, Kill = {}
Q15. Which of the following is correct gen and kill sets of basics block B4 for reaching
definition analysis?
(a) Gen = {d7}, Kill = {d1, d4} (b) Gen = {d7}, Kill = {d1, d4, d7}
(c) Gen = {d7}, Kill = {d4, d5} (d) Gen = {d7}, Kill = {d1, d4, d6}
Q16. Which of the following is correct in and out sets of basics block B1 for reaching
definition analysis?
(a) In = {}, Out = {d1,d2,d3} (b) In = {d1}, Out = {d1,d2}
(c) In = {}, Out = {d1,d2} (d) In = {}, Out = {d1,d3}
Q17. Which of the following is correct in and out sets of basics block B2 for reaching
definition analysis?
(a) In = {d1,d2,d3}, Out = {d3,d4,d5}
(b) In = {d1,d2,d3,d5,d6}, Out = {d3,d4,d5,d6}
(c) In = {d1,d2,d3,d5,d6,d7}, Out = {d3,d4,d5,d6}
(d) In = {d1,d2,d3,d5}, Out = {d3,d4,d5,d6}
Q18. Which of the following is correct in and out sets of basics block B3 for reaching
definition analysis?
(a) In = {d3, d4, d5}, Out = {d4, d5, d6}
(b) In = {d3, d4, d5, d6}, Out = {d4, d5, d6}
(c) In = {d3, d4, d5}, Out = {d4, d5}
(d) In = {d3, d4, d5}, Out = {d4, d5, d6}
Q19. Which of the following is correct in and out sets of basics block B4 for reaching
definition analysis?
(a) In = {d3,d4,d5,d6}, Out = {d3,d5,d6,d7}
(b) In = {d3,d4,d5,d6}, Out = {d3,d4,d5}
(c) In = {d3,d4,d5,d6}, Out = {d3,d4,d5,d6}
(d) In = {d3,d4,d5,d6}, Out = {d3,d4,d5,d6}
Q20. Which of the following is correct use and define sets of basics block B1 for live variable
analysis?
(a) Use = { m,n}, Def = {i,j,a } (b) Use = { m,n,u1}, Def = {i,j }
(c) Use = {m,n }, Def = {i,j } (d) Use = { m,n,u1 }, Def = { i,j,a}
Q21. Which of the following is correct use and define sets of basics block B2 for live variable
analysis?
(a) Use = {i,j }, Def = {i,j } (b) Use = { i,j}, Def = {i }
(c) Use = {i,j }, Def = { } (d) Use = {i,j }, Def = {j }
Q22. Which of the following is correct use and define sets of basics block B3 for live variable
analysis?
(a) Use = {u2 }, Def = {a } (b) Use = {u2 }, Def = {u2,a }
(c) Use = { a,u2}, Def = {a } (d) Use = {a }, Def = { u2}
Q23. Which of the following is correct use and define sets of basics block B4 for live variable
analysis?
(a) Use = {a,j,i }, Def = {i } (b) Use = { a,j}, Def = {i }
(c) Use = {a,j,i }, Def = {a,j,i } (d) Use = {a,j,i }, Def = { i,j}
Q24. Which of the following is correct in and out sets of basics block B1 for live variable
analysis?
(a) In = {m,n,u1}, Out = {i,j,u2,a} (b) In = {m,n}, Out = {i,j,u2,a}
(c) In = {m,n,u1}, Out = {i,j,u2} (d) In = {m,n}, Out = {i,j,u2}
Q25. Which of the following is correct in and out sets of basics block B2 for live variable
analysis?
(a) In = {i,j,a}, Out = {a,j,u2} (b) In = {i,j,a,u2}, Out = {a,j,u2}
(c) In = {i,j,a}, Out = {a,j,u1} (d) In = {i,j,a,u1}, Out = {a,j,u2}
Q26. Which of the following is correct in and out sets of basics block B3 for live variable
analysis?
(a) In = {j,u2}, Out = {a,j,u2} (b) In = {i,u2}, Out = {a,j,u2}
(c) In = {i,j,u2}, Out = {a,j,u2} (d) In = {i,j}, Out = {a,j,u2}
Q27. Which of the following is correct in and out sets of basics block B4 for live variable
analysis?
(a) In = {a,j,u2}, Out = {a,j,u2} (b) In = {a,j,u2}, Out = {a,i,j,u2}
(c) In = {a,j,u2}, Out = {a,i,u2} (d) In = {a,i,u2}, Out = {a,i,j,u2}
For next two questions: Consider the following basic block for live variables:
Assume you are given IN/OUT for B1, B2, B4, B5, and GEN/KILL for B3.
Q30. What is the forward data-flow problems for available expression for B3 ?
(A) IN(B3) = OUT(B1) OUT(B2) , OUT(B3) = GEN(B3) (IN(B3) – KILL(B3))
(B) IN(B3) = OUT(B1) OUT(B2), OUT(B3) = GEN(B3) U (IN(B3) – KILL(B3))
(C) IN(B3) = OUT(B1) OUT(B2), OUT(B3) = GEN(B3) U (IN(B3) – KILL(B3))
(D) IN(B3) = OUT(B1) OUT(B2) ,OUT(B3) = GEN(B3) (IN(B3) – KILL(B3))
Q31. What is the forward data-flow problems for reaching definition for B3 ?
(A) IN(B3) = OUT(B1) OUT(B2) , OUT(B3) = GEN(B3) (IN(B3) – KILL(B3))
(B) IN(B3) = OUT(B1) OUT(B2), OUT(B3) = GEN(B3) U (IN(B3) – KILL(B3))
(C) IN(B3) = OUT(B1) OUT(B2), OUT(B3) = GEN(B3) U (IN(B3) – KILL(B3))
(D) IN(B3) = OUT(B1) OUT(B2) ,OUT(B3) = GEN(B3) (IN(B3) – KILL(B3))
Calculate GEN/KILL for each basic block. Which of the following option is true?
Q36. What is the minimal number of registers necessary for the generation of code
corresponding with the following expression (if one operand can be a memory location)
(a * b) + (c - (d + e))? __________
Q37. What is the minimal number of registers necessary for the generation of code
corresponding with the following expression (if all operands in cpu register)
` (a * b) + (c - (d + e))?
(A) 2 (B)3 (C)4 (D)5
Q38. Consider the following code
a=1;
b = 10;
c = 20;
d = a + b;
e = c + d;
f = c + e;
b = c + e;
e = b + f;
d = 5 + e;
return d + f;
What is the fewest number of registers that is needed for this program, without
spilling? _____
Q1. Answer: 7
Solution:
Q2. Answer: a, b, d
Solution: Instructions 5, 6 & 8 are loop-invariant instruction.
Q3. Answer: A
Solution: Only instruction 5 can be moved out of the loop. Because
both instruction 6 and instruction 8 define y, they cannot be moved.
Q4. Answer: c
Q5. Answer: c
Q6. Answer: d
Q7. Answer: d
Solutions of Q4, Q5, Q6, Q7
Q8. Answer: a
Q9. Answer: b
Q10. Answer: a
Q11. Answer: c
Solutions of Q8, Q9, Q10, Q11
Q12. Answer: b
Q13. Answer: d
Q14. Answer: a
Q15. Answer: a
Solutions of Q12, Q13, Q14, Q15
Q16. Answer: a
Q17. Answer: c
Q18. Answer: b
Q19. Answer: a
Solutions of Q16, Q17, Q18, Q19
Q20. Answer: d
Q21. Answer: c
Q22. Answer: a
Q23. Answer: b
Q24. Answer: a
Q25. Answer: b
Q26. Answer: a
Q27. Answer: b
Solutions of Q20, Q21, Q22, Q23, Q24, Q25, Q26, Q27
Q28. D
Q29. C
Q30. C
Q31. B
Q32. D
Q33. Answer: B
Solution
IN[B] = ∩ P a predecessor of B OUT[P];
OUT[B] =gen[B] ∪ [ (IN[B] − kill[B]);
Q34. Answer: D
Q35. Answer: a, c
Solution:
We can do left shift n<<4 at line 2 - strength reduction.
Line 3 there is induction variable elimination a s constant is subtracting.
2 statements are correct.
Q36. Answer: 2
Solution: