Compiler Optimizations and Control Flow Graph Analysis, Exams of Computer Science

Practice questions related to compiler optimizations and control flow graph analysis. The questions cover topics such as compiler transformations, local optimizations, reaching definitions, live variables, available expressions, and instruction scheduling. Students are asked to build a dag for a code segment, perform control flow analysis, calculate reaching definitions and live variables, and determine available expressions and instruction schedules.

Typology: Exams

Pre 2010

Uploaded on 02/13/2009

koofers-user-zxh
koofers-user-zxh 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 430 Practice 3
These 3-address code instructions may be used in the follow-
ing questions:
3-addr Instruction Eect
load R1 x R1
x
store x R1 x
R1
add R1 R2 R3 R1
R2+R3
sub R1 R2 R3 R1
R2-R3
mult R1 R2 R3 R1
R2 * R3
1. Optimizations
(a) How can compiler transformations improve a pro-
gram?
(b) What does the compiler need to consider when ap-
plying optimizations?
(c) What are the dierent scopes of compiler optimiza-
tions? What are the tradeos when considering
what scope of optimizations to use?
2. Local optimizations
Consider the following code.
(1) a := 1
(2) b := f + a
(3) c := a
(4) d := f + a
(5) e := f + c
(6) f := b
(7) g := f + a
(a) Build a DAG for the code.
3. Control ow analysis
For the following problems, consider this code:
<S1> a := 1
<S2> b := 2
<S3> L1: c := a + b
<S4> d := c - a
<S5> if (...) goto L3
<S6> L2: d := b * d
<S7> if (...) goto L3
<S8> d := a + b
<S9> e := e + 1
<S10> goto L2
<S11> L3: b := a + b
<S12> e := c - a
<S13> if (...) goto L1
<S14> a := b * d
<S15> b := a - d
(a) What are the basic blocks?
(b) What is the control ow graph?
(c) Depth-rst order selects nodes in the order they
are visited (start by visiting the root node) and
then recursively visiting every child of each node
(if the child has not been visited before). Note that
the order in whichchildren are visited is random.
What are all the possible results of depth-rst traver-
sal on the control ow graph?
(d) Using depth-rst order, is it possible to visit a child
before its parent? For which depth-rst ordering(s)
of the control ow graph does this occur?
(e) Postorder selects nodes (starting from root)
after
visiting every child of that node (if the child has
not been visited before). Note that the order in
whichchildren are visited is random.
What are all the possible results of Postorder traver-
sal for the control ow graph?
(f) Reverse Postorder simply reverses the no de order-
ing found by a Postorder traversal of the graph.
What are the possible Reverse Postorder traver-
sals of the control ow graph?
(g) Using Reverse Postorder, is it possible to visit a
child before its parent? Whyorwhy not?
4. Reaching denitions
Reaching denitions
for a point in the program
p
is de-
ned as the set of denitions of a variable for which there
is some path from the denition to
p
with no other def-
inition of that variable. Calculate reaching denitions
for the code in the control-ow graph problem.
(a) What is the dataow equation for REACH?
(b) In what direction is REACH calculated? I.e., do es
information ow forwards or backwards in the CFG?
(c) Calculate GEN, KILL for each basic block.
(d) What is a good initial value for REACH for each
basic block?
(e) Solve the data-ow equations in reverse Postorder.
Showyour work.
5. Livevariables
Live variables
for a point in the program
p
is dened
as the set of variables
x
for which there is some path
from
p
to a use of
x
with no denition to
x
on the path.
Calculate livevariables for the code in the control-ow
graph problem.
(a) We dene LIVE(
b
) for a basic block
b
to be the
set of live variables at the end of
b
. What is the
dataow equation for LIVE?
(b) In what direction is LIVE calculated? I.e., do es in-
formation ow forwards or backwards in the CFG?
(c) Show GEN, KILL for each basic block.
(d) What is a good initial value for LIVE for each basic
block?
(e) Solve the data-ow equations in rPostorder. Show
your work.
pf2

Partial preview of the text

Download Compiler Optimizations and Control Flow Graph Analysis and more Exams Computer Science in PDF only on Docsity!

CMSC 430 Practice 3

These 3-address co de instructions may b e used in the follow- ing questions:

3-addr Instruction E ect load R1 x R1 x store x R1 x R add R1 R2 R3 R1 R2 + R sub R1 R2 R3 R1 R2 - R mult R1 R2 R3 R1 R2 * R

  1. Optimizations

(a) How can compiler transformations improve a pro- gram? (b) What do es the compiler need to consider when ap- plying optimizations? (c) What are the di erent scop es of compiler optimiza- tions? What are the tradeo s when considering what scop e of optimizations to use?

  1. Lo cal optimizations Consider the following co de.

(1) a := 1 (2) b := f + a (3) c := a (4) d := f + a (5) e := f + c (6) f := b (7) g := f + a

(a) Build a DAG for the co de.

  1. Control ow analysis For the following problems, consider this co de:

a := 1 b := 2 L1: c := a + b d := c - a if (...) goto L L2: d := b * d if (...) goto L d := a + b e := e + 1 goto L L3: b := a + b e := c - a if (...) goto L a := b * d b := a - d

(a) What are the basic blo cks? (b) What is the control ow graph?

(c) Depth- rst order selects no des in the order they are visited (start by visiting the ro ot no de) and then recursively visiting every child of each no de (if the child has not b een visited b efore). Note that the order in which children are visited is random. What are all the p ossible results of depth- rst traver- sal on the control ow graph? (d) Using depth- rst order, is it p ossible to visit a child b efore its parent? For which depth- rst ordering(s) of the control ow graph do es this o ccur? (e) Postorder selects no des (starting from ro ot) after visiting every child of that no de (if the child has not b een visited b efore). Note that the order in which children are visited is random. What are all the p ossible results of Postorder traver- sal for the control ow graph? (f ) Reverse Postorder simply reverses the no de order- ing found by a Postorder traversal of the graph. What are the p ossible Reverse Postorder traver- sals of the control ow graph? (g) Using Reverse Postorder, is it p ossible to visit a child b efore its parent? Why or why not?

  1. Reaching de nitions Reaching de nitions for a p oint in the program p is de- ned as the set of de nitions of a variable for which there is some path from the de nition to p with no other def- inition of that variable. Calculate reaching de nitions for the co de in the control- ow graph problem.

(a) What is the data ow equation for REACH? (b) In what direction is REACH calculated? I.e., do es information ow forwards or backwards in the CFG? (c) Calculate GEN, KILL for each basic blo ck. (d) What is a go o d initial value for REACH for each basic blo ck? (e) Solve the data- ow equations in reverse Postorder. Show your work.

  1. Live variables Live variables for a p oint in the program p is de ned as the set of variables x for which there is some path from p to a use of x with no de nition to x on the path. Calculate live variables for the co de in the control- ow graph problem.

(a) We de ne LIVE(b) for a basic blo ck b to b e the set of live variables at the end of b. What is the data ow equation for LIVE? (b) In what direction is LIVE calculated? I.e., do es in- formation ow forwards or backwards in the CFG? (c) Show GEN, KILL for each basic blo ck. (d) What is a go o d initial value for LIVE for each basic blo ck? (e) Solve the data- ow equations in rPostorder. Show your work.

  1. Available expressions

Available expressions is a data- ow analysis problem whose solution is used to guide global common sub ex- pression. It calculates AVAIL, the expressions available at the b eginning of each basic blo ck. Consider the following co de. Assume that b+c is the only expression of interest:

B

B

B2 B

a := b+c

d := b+c b := 2e := b+c

(a) What is the data- ow equation for AVAIL? (b) Give GEN and KILL (needed by AVAIL) for each basic blo ck. (c) What is a go o d initial value for AVAIL for each basic blo ck? (d) Calculate AVAIL. Show all steps, including values for AVAIL and the order basic blo cks are analyzed.

  1. Data- ow lattices

Prove the following prop erties of lattices:

(a) Show that a  b and b  c implies a  c

(b) Show that a  (b ^ c) implies a  b

  1. Data- ow frameworks

(a) When estimating each of the following sets, tell whether to o-large or to o-small estimates are con- servative. Explain your answer in terms of the in- tended use of information. i. Available expressions ii. Reaching de nitions iii. Live variables (b) What prop erties are necessary to ensure an itera- tive data- ow analysis framework terminates? (c) What prop erties are necessary to ensure an itera- tive data- ow analysis framework terminates with the meet-over-all-paths solution?

  1. Instruction scheduling Consider scheduling the co de b elow using list schedul- ing. All instructions must complete b efore executing the jmp instruction. Assume the following instruction latencies:

 2-cycle latency for load  1-cycle latency otherwise

<dst, s1, s2> 1 load r1, a 2 add r2, r1, # 3 store x, r 4 load r3, b 5 mult r4, r3, r 6 load r1, c 7 add r5, r1, r 8 store y, r 9 load r6, d 10 mult r7, r5, # 11 store z, r 12 jmp

(a) Build the precedence graph for the instructions. Mark dep endences as ow, anti, or output. You can ignore transitive dep endences. (b) Calculate the critical path for the instructions. (c) Schedule the instructions for a single-issue pro ces- sor, using forward list scheduling. Show candidates instructions at each cycle. Prioritize candidates using 1) critical path, 2) latency of instruction, 3) numb er of children. (d) Schedule the instructions as ab ove, for a two-issue VLIW pro cessor. (e) How could you change register assignments to im- prove instruction schedules in the co de?