Download Exam 3 Solutions - Introduction to Compilers | CMSC 430 and more Exams Computer Science in PDF only on Docsity!
CMSC 430 Practice 3
Solutions
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
- Optimizations
(a) How can compiler transformations improve a pro- gram?
By reducing program size or number of instructions
executed, taking advantage of redundant computa-
tions, customizing general purpose code, or undo-
ing high-level abstractions.
(b) What do es the compiler need to consider when ap- plying optimizations?
Safety, pro tability, and applicability.
(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?
Peephole, local, global, or interprocedural. The ba-
sic tradeo is more complexity and compile time
for optimizations with greater scope.
- 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.
b, d, e, f
c, a
g
f 1
Note that assignment gives a node multiple labels,
and may require renaming variables.
- 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?
B1 = f S1, S2 g
B2 = f S3, S4, S5 g
B3 = f S6, S7 g
B4 = f S8, S9, S10 g
B5 = f S11, S12, S13 g
B6 = f S14, S15 g
(b) What is the control ow graph?
B
B
B
B
B
B
(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?
B1, B2, B3, B4, B5, B
B1, B2, B5, B6, B3, B
B1, B2, B3, B5, B6, B
(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?
If loops are present, any node ordering wil l cause
a node to be visited before al l of its parents.
If backward loop edges (i.e., B5!B2, B4!B3) are
ignored, then it is stil l possible to visit a child before
its parent using depth- rst ordering. The depth-
rst ordering in which this occurs is:
B1, B2, B5, B6, B3, B
(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?
B6, B5, B4, B3, B2, B
B4, B6, B5, B3, B2, B
(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?
B1, B2, B3, B4, B5, B
B1, B2, B3, B5, B6, B
(g) Using Reverse Postorder, is it p ossible to visit a child b efore its parent? Why or why not?
If loops are present, any node ordering wil l cause
a node to be visited before al l of its parents.
If backward loop edges (i.e., B5!B2, B4!B3) are
ignored, then it is not possible to visit a child before
its parent using reverse postorder ordering.
This is because postorder wil l number a node only
after al l its children have been visited, guaranteeing
a node wil l always be assigned a number larger than
al l of its children/descendents. Once the ordering
is reversed, a node is guaranteed to have a smal ler
number than al l of its children/descendents.
- 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?
REACH(b) =
S
x 2 pr ed(b) (GEN(x)[(REACH^ (x) KILL(x)))
(b) In what direction is REACH calculated? I.e., do es information ow forwards or backwards in the CFG?
Forwards.
(c) Calculate GEN, KILL for each basic blo ck.
Note: For reaching de nitions, we append to each
variable de nition its location to distinguish be-
tween multiple de nitions to the same variable.
Blo ck GEN KILL B1 a1,b2 a1,b2,b11,a14,b B2 c3,d4 c3,d4,d6,d B3 d6 d4,d6,d B4 d8,e9 d4,d6,d8,e9,e B5 b11,e12 b2,e9,b11,e12,b B6 a14,b15 a1,b2,b11,a14,b
(d) What is a go o d initial value for REACH for each basic blo ck?
REACH for each basic block is initialized to ; (no
reaching de nitions).
(e) Solve the data- ow equations in reverse Postorder. Show your work.
Solving the data ow equations in reverse postorder
(B1,B2,B3,B4,B5,B 6), we get the fol lowing:
Blo ck Data Initial Pass1 Pass B1 IN ; ; ; OUT ; a1,b2 a1,b B2 IN ; a1,b2 a1,b2,c3,d4,d6,b11,e OUT ; a1,b2,c3,d4 a1,b2,c3,d4,b11,e B3 IN ; a1,b2,c3,d4 a1,b2,c3,d4,d8,e9,b11,e OUT ; a1,b2,c3,d6 a1,b2,c3,d6,e9,b11,e B4 IN ; a1,b2,c3,d6 a1,b2,c3,d6,e9,b11,e OUT ; a1,b2,c3,d8,e9 a1,b2,c3,d8,e9,b B5 IN ; a1,b2,c3,d4,d6 a1,b2,c3,d4,d6,e9,b11,e OUT ; a1,c3,d4,d6,b11,e12 a1,c3,d4,d6,b11,e B6 IN ; a1,c3,d4,d6,b11,e12 a1,c3,d4,d6,b11,e OUT ; c3,d4,d6,e12,a14,b15 c3,d4,d6,e12,a14,b
- 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?
LIVE(b) =
S
x 2 succ(b) (GEN^ (x)^ [^ (LIVE^ (x)^ ^ KILL(x)))
- Data- ow lattices
Prove the following prop erties of lattices:
(a) Show that a b and b c implies a c
First, by de nition of , we see a b gives us
a ^ b = a.
Similarly, from b c we get b ^ c = b.
Taking a ^ b = a and replacing b with (b ^ c), we
get a ^ (b ^ c) = a.
Since ^ is associative, we nd a ^ (b ^ c) = (a ^
b) ^ c = a ^ c.
But since we also have a ^ (b ^ c) = a, this implies
a ^ c = a.
By de nition of , this implies a c.
(b) Show that a (b ^ c) implies a b
First, we show (b ^ c) b.
By de nition of , this is true if b ^ (b ^ c) = b ^ c.
Since ^ is associative, we prove it using b ^ (b ^ c) =
(b ^ b) ^ c = b ^ c.
Now combining a (b ^ c) and (b ^ c) b gives us
a b, using the solution to the previous problem.
- 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
Too-smal l estimates are conservative (? = ;).
Common subexpression elimination would not
replace some expressions if the estimate is too
smal l, but the answer would be stil l correct.
In comparison, performing CSE when an ex-
pression is not available may yield incorrect
results.
ii. Reaching de nitions
Too-large estimates are conservative
(? = f al l copy statements g ).
Copy propagation would be restricted if the es-
timate of copy statements reaching a point is
too large, but the result would stil l be correct.
In comparison, estimating too few copy state-
ments reach a given point may enable copy
propagation when the right-hand side of a miss-
ing copy statement di ers from the right-hand
side of the copy statements found, yielding in-
correct results.
iii. Live variables
Too-smal l estimates are conservative (? = ;).
The opposite of variables changed by a proce-
dure. Variables not modi ed by a procedure
are left out of the KILL set for available ex-
pressions. Adding too few variables would re-
sult in a too-smal l estimate for available ex-
pressions, yielding a conservative approach to
common subexpression elimination.
(b) What prop erties are necessary to ensure an itera- tive data- ow analysis framework terminates?
The data ow problem must be monotonic,
I.e., f (x ^ y ) f (x) ^ f (y ). Al l chains (sequences
of less-than orderings) in the lattice must also have
nite length (true for any lattice with nite height).
(c) What prop erties are necessary to ensure an itera- tive data- ow analysis framework terminates with the meet-over-all-paths solution?
The data ow problem must be distributive. I.e.,
f (x ^ y ) = f (x) ^ f (y ).
- 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.
Anti
(b) Calculate the critical path for the instructions.
The fol lowing graph shows the critical path for a
node as a number next to the node. The number
inside a node indicates the latency of its instruc-
tion.
1
2 3
2
4
6 2
6
2
7
9
3
(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.
1-issue 2-issue
Time Cand Inst Cand Inst
(d) Schedule the instructions as ab ove, for a two-issue VLIW pro cessor.
See above. Results show that issuing two instruc-
tions per cycle is suÆcient to achieve maximum
performance (equal to critical path).
(e) How could you change register assignments to im- prove instruction schedules in the co de?
Using a new register instead of r 1 in instruction
6 & 7 would avoid an antidependence between in-
struction 2 & 6.