0% found this document useful (0 votes)
9 views19 pages

CD Practice Question of Data Flow Analysis

The document contains a series of questions related to code optimization, specifically focusing on basic blocks, loop-invariant instructions, gen and kill sets, and data-flow analysis in control flow graphs. It includes multiple-choice questions that assess understanding of available expression analysis, reaching definition analysis, and live variable analysis. The questions require identifying correct sets and analyzing code structures for optimization purposes.
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)
9 views19 pages

CD Practice Question of Data Flow Analysis

The document contains a series of questions related to code optimization, specifically focusing on basic blocks, loop-invariant instructions, gen and kill sets, and data-flow analysis in control flow graphs. It includes multiple-choice questions that assess understanding of available expression analysis, reaching definition analysis, and live variable analysis. The questions require identifying correct sets and analyzing code structures for optimization purposes.
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/ 19

Code optimization

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)

Q2. What are the loop-invariant instructions in this code? [MSQ]


(a) Instructions 5 (b) Instruction 6
(c) Instruction 7 (d) Instruction 8

Q3. Which instructions could be moved out of the loops?


(a) Instructions 5 (b) Instruction 6
(c) Instruction 7 (d) Instruction 8
For next Eight questions: Consider the following CFG

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}

For next Eight questions: Consider the following CFG

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:

Q28. What is USE for the basic block?


(A) {a} (B) {b,c} (C) {e,d} (D) { a, c }

Q29. What is DEF for the basic block?


(A) {a,b,d} (B) {c,d,b} (C) { a, b, d, e } (D) {a, c}
Consider the following control flow graph for next three questions

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))

Q32. What is the backwards data-flow problems for B3 ?


(A) OUT(B3) = IN(B4) IN(B5) , IN(B3) = USE(B3) U (OUT(B3) – DEF(B3))
(B) OUT(B3) = IN(B4) IN(B5), IN(B3) = USE (B3)  (OUT(B3) – DEF (B3))
(C) OUT(B3) = IN(B4) IN(B5) , IN(B3) = USE (B3)  (OUT(B3) – DEF (B3))
(D) OUT(B3) = IN(B4) IN(B5) ,IN(B3) = USE (B3) U (OUT(B3) – DEF (B3))
Q33. Consider the following control flow graph for available expressions

Calculate GEN/KILL for each basic block. Which of the following option is true?

Q34. Consider this code:


<S1> a := b
<S2> L1: b := c
<S3> if (...) goto L2
<S4> c := d
<S5> if (...) goto L1
<S6> L2: d := a
How many basic blocks are there?
(A) 3 (B) 4 (C) 5 (D) 6
Q35. Consider the following program fragment.
1: j = n
2: i = 4 * n
3: j= j - 1
4: y = 4 * j
5: z = M[y]
6: i = i + z
7: if j > 0 goto 3

How many of the statements are correct:__________


(a) There is scope for applying strength reduction.
(b) There is scope for applying copy propagation.
(c) There is scope for applying induction variable elimination.
(d) There is scope for applying dead code elimination.

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:

Q37. Answer: B Solution:


Q38. Answer: 3
a=1; a=1;
b = 10; b = 10;
c = 20; c = 20;
d = a + b; d = a + b;
e = c + d; e = c + d;
f = c + e; f = c + e;
b = c + e; e = 2*f
e = b + f; d = 5 + e;
d = 5 + e; return d+f;
return d+f;
We can reduce the expression b = c + e
And replace e = b + f to e = 2*f

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy