Docsity

Homework Solutions for Advanced Compilers | CS 640, Assignments of Computer Science

Toggle sidebar
bg1
CS 640 HW #2
Fall 2008
Due Oct 6 at classtime
1. Dataflow analysis can be used to propagate constants in a CFG. Give a dataflow
algorithm for this – be sure to provide details regarding the transfer functions (and
how they are computed). Show how your algorithm works on CFG1, including
the contents of sets after each iteration. I know some this is available in various
textbooks, but try to work out the details yourself.
Constant Propagation data flow:
Domain: We need to keep track of both what variables are constant AND
their current value. This means we are going to have a set of tuples <var,val>
as our domain. We are also going to keep track of when we know that a
variable is not a constant using a N in place of the value. And finally, in order
to make the analysis work out correctly, we are going to also us an undefined
notation U
Direction: forward
Equation across blocks: Const_In(B)= ^ xÎpred(B) Const_Out(x), where we
define this function over tuples as:
<x,C> ^ <x,C> = <x,C> for constant C
<x,C> ^ <x,C’> = <x,N> for different constants C and C’
<x,C> ^ <x,N> = <x,N> for constant C
<x,C> ^ <x,U> = <x,C> for constant C
<x,N> ^ <x,U> = <x,N>
<x,N> ^ <x,N> = <x,N>
<x,U> ^ <x,U> = <x,U>
Transfer function -- Finding a way to compute Const_Out is the trickiest part
of this question. We want to be sure that any constant explicitly defined by
the block is in this set, plus any constants that enter the block and are not
killed. In addition, we can create new constants in the block when constant
values enter the block. For the statement, b:=a, we might not know that b is a
constant until the fact that a is a constant finally becomes part of Const_In.
For this reason, I’m going to initialize Const_Out as Const_In and then define
the transfer function based on the way that the individual statements modify
the Const_Out set (in a forward direction).
Copy statements: b := a - update <b,Z> where <a,Z> is in Const_Out
(where Z can be a constant, N or U)
Binary operator: z := x op y – if x and y are constants c1 and c2, then
update z to be constant = c1 op c2. If either x or y are not constants
(N), then z is not a constant N. Otherwise, z is U
Non-assignment statements do not modify Const_Out
Initialize all blocks Const_In(b) = {<x,U> such that x is a variable in the
program}
pf3
pf4
pf5
pf8

Partial preview of the text

Download Homework Solutions for Advanced Compilers | CS 640 and more Assignments Computer Science in PDF only on Docsity!

CS 640 HW

Fall 2008

Due Oct 6 at classtime

  1. Dataflow analysis can be used to propagate constants in a CFG. Give a dataflow algorithm for this – be sure to provide details regarding the transfer functions (and how they are computed). Show how your algorithm works on CFG1, including the contents of sets after each iteration. I know some this is available in various textbooks, but try to work out the details yourself. Constant Propagation data flow:  Domain: We need to keep track of both what variables are constant AND their current value. This means we are going to have a set of tuples <var,val> as our domain. We are also going to keep track of when we know that a variable is not a constant using a N in place of the value. And finally, in order to make the analysis work out correctly, we are going to also us an undefined notation U  Direction: forward

 Equation across blocks: Const_In(B)= ^ xÎpred(B) Const_Out(x), where we

define this function over tuples as:  <x,C> ^ <x,C> = <x,C> for constant C  <x,C> ^ <x,C’> = <x,N> for different constants C and C’  <x,C> ^ <x,N> = <x,N> for constant C  <x,C> ^ <x,U> = <x,C> for constant C  <x,N> ^ <x,U> = <x,N>  <x,N> ^ <x,N> = <x,N>  <x,U> ^ <x,U> = <x,U>  Transfer function -- Finding a way to compute Const_Out is the trickiest part of this question. We want to be sure that any constant explicitly defined by the block is in this set, plus any constants that enter the block and are not killed. In addition, we can create new constants in the block when constant values enter the block. For the statement, b:=a, we might not know that b is a constant until the fact that a is a constant finally becomes part of Const_In. For this reason, I’m going to initialize Const_Out as Const_In and then define the transfer function based on the way that the individual statements modify the Const_Out set (in a forward direction).  Copy statements: b := a - update <b,Z> where <a,Z> is in Const_Out (where Z can be a constant, N or U)  Binary operator: z := x op y – if x and y are constants c1 and c2, then update z to be constant = c1 op c2. If either x or y are not constants (N), then z is not a constant N. Otherwise, z is U  Non-assignment statements do not modify Const_Out  Initialize all blocks Const_In(b) = {<x,U> such that x is a variable in the program}

  1. The two CFGs below have been annotated with assignments for variable x. Show where -functions need to be added for SSA form and show the appropriate variable renaming. Once you have done this, show the transformation back to executable code (i.e. no  functions). For the first CFG, we have assignments to x in {a,c,g,f}. DF({a,c,g,f}) = {e,c,f,h}, so we need a function in all of these. DF({e,c,f,h}) adds no new places. For the second CFG, we have assignments to x in {b,g,h}. DF({b,g,h}) = {k,d,f}. Note that I put 3 parameters on the  function in k, since it has three predecessors. Strictly speaking, this isn’t necessary since only two values reach this point, but from an implementation standpoint, it makes life easier. a = 2 b = 3 c = a + b d = a + a b = a e = a + b a = a + 1 b = d + f c = 3 c = 3 f = a + c b = 5 b = d + f e = e + 1 e = d + 1 f = e + c {<a,U>,<b,U>,<c,U>,<d,U>,<e,U>,<f,U>} {<a,2>,<b,2>,<c,5>, <d,4>,<e,4>,<f,U>} {<a,N>,<b,N>,<c,N>, <d,4>, <e,4>,<f,5>} {<a,N>,<b,9><c,3>, <d,4>, <e,4>,<f,5>} (^) {<a,N>,<b,9>,<c,3>,<d,4>,< e,N><f,5>} {<a,N>,<b,9>,<c,3>,<d,4>,<e,5>, <f,8>} {<a,2>,<b,2>,<c,5>,<d,4>, <e,4>,<f,U>} {<a,2>,<b,5>,<c,3>,<d,4>, <e,4>,<f,5>} {<a,2>,<b,5>,<c,3>, <d,4>, <e,4>,<f,5>} {<a,2>,<b,9>,<c,3>, <d,4>, <e,5>,<f,5>}

a b c d e f g h a i x1 = x4 = x1,x5 x5 = x7 = x6 = x3,x1 x8 = x6,x5 x2 = x7,x1 x3 = Visit order based on DFS of IDOM tree: a, b, c, d, f, i, e, g, h (not the only possible ordering) j a b c d e f g h k i x1 = x5 = x2 = x3 = x1,x5 x4 = x3,x5 x6 = x2x2,x3 Visit order based on DFS of IDOM tree: a, b, c, e, g, i, d, f, h, j, k (not the only possible ordering)

a b c d e f g h a i x1 = x4 = x1,x5 x5 = x8 = x

x7 = x2 = x x6 = x3,x1 x8 = x6* x8 = x6,x5 x2 = x1* x6 = x1* x4 = x1* x4 = x x2 = (x1,x7) x3 = x6 = x Some of these are on critical edges and so should sit in their own blocks but I was too lazy. Those assignments are marked with * j a b c d e f g h k i x1 = x3 = x1* x5 = x2 = x4 = x5* x6 = x x3 = x1,x5 x6 = x3* x4 = x3* x4 = x3,x5 x6 = x2x2,x3 x3 = x

Block IDom DF 1 - - 2 1 3. 3 1 3, 4 2 5 5 1 a1 = 2 b1 = 3 c1 = a1 + b d1 = a1 + a b2 = a e1 = a1 + b a2 =(a3,a1,a1)a3,a1,a1) b5 =(a3,a1,a1)b6,b2,b3) c3 =(a3,a1,a1)c4,c1,c2) f2 =(a3,a1,a1)f2,f0,f1) a3 = a2 + 1 b6 = d1 + f c4 = 3 c2 = 3 f1 = a1 + c b3 = 5 b4 = d1 + f e2 = e1 + 1 a4 =(a3,a1,a1)a3,a1) b7 =(a3,a1,a1)b6,b4) c5 =(a3,a1,a1)c4,c2) e3 =(a3,a1,a1)e1,e2) f3 =(a3,a1,a1)f2,f1) e4 = d1 + 1 f4 = e4 + c

B

B

B3 B

B

var C va r C va r C va r C var C a1 2 b1 3 c1 5 e1 4 f1 5 a2 b2 2 c2 3 e2 5 f a3 b3 5 c3 e3 f a4 b4 9 c4 3 e4 5 f4 8 b5 c5 3 b d1 4 b a1 = 2 b1 = 3 c1 = 5 a1 + b d1 = 4 a1 + a b2 = 2 a e1 = 4 a1 + b a2 =(a3,a1,a1)a3,a1,a1) b5 =(a3,a1,a1)b6,b2,b3) c3 =(a3,a1,a1)c4,c1,c2) f2 =(a3,a1,a1)f2,f0,f1) a3 = a2 + 1 b6 = d1 + f c4 = 3 c2 = 3 f1 = 5 a1 + c b3 = 5 b4 = 9 d1 + f e2 = 5 e1 + 1 a4 =(a3,a1,a1)a3,a1) b7 =(a3,a1,a1)b6,b4) c5 = 3(a3,a1,a1)c4,c2) e3 =(a3,a1,a1)e1,e2) f3 =(a3,a1,a1)f2,f1) e4 = 5 d1 + 1 f4 = 8 e4 + c

B

B

B3 B

B