• Tidak ada hasil yang ditemukan

2.3 Dynamic Single Assignment

2.3.4 Selection Statements

Selection statements contain different branches of control, of which only one is executed per program iteration. The command of each branch (guarded command) can be straightline code containing numerous assignments, or even more general code that can also include nested selections or loops.

Recalling Definition (6) for DSA programs, a selection in a DSA program can therefore contain multiple assignments to the same variable, as long as no more than one assignment appears in each

guarded command.

Let us consider only selection statements with straightline code as commands for now, and build up the more general solution later in Section 2.3.7. For the ith branch in a selection G, we label the guard condition Gi and the command (straightline code) Si. Our method for converting a stand-alone selection statement into DSA form is to apply the straightline code DSA transformation independently to every commandSi in G. When this method is applied to

G ≡ [ G1−→ S1 [] ... [] Gi −→ Si [] ... ] the resulting structure is

GDSA ≡ [ G1−→ (S1)DSA [] ... [] Gi−→ (Si)DSA [] ... ]

If the selection does stand alone then this transformation is in fact enough. However, selections generally appear in the middle of a larger series of statements; variables used in a selection can have initial values assigned prior to the selection and can also be used in other computations after the selection. DDD must therefore take additional steps to ensure that the entire DSA program is correct.

Throughout the rest of this section, we will illustrate different steps referring to the CHP pro- grams P1, P2 and P3 from Figure 2.3. We label the selection statement in each program “G.”

The code may not be that efficient from a programming perspective, but it serves well as a simple example for DSA transformation!

2.3.4.1 DSA Indices from Pre-Selection Code

When transforming a selection statementGto DSA form, we must replace all variablesvthat appear in the guard conditions ofG with the DSA variablesvNG

0[v]. This makes the indexing of new DSA variables in the selection statement consistent with DSA indices used in any pre-selection code.

In Figure 2.3, this transformation is among those applied to P1resulting in new selection state- mentP2. The initial DSA indices used for variables inG are

N0G[a] =N0G[b] =N0G[c] = 1

P1≡*[ A?a, B?b, C?c;

[ a∧b∧c−→ C?c; D!c; c:= 5; f := 2*c; F!f [] a∧ ¬b−→ G?c; D!c

[] else−→ skip ];

X!a, Y!c ]

P2≡*[ A?a1, B?b1, C?c1;

[ a1∧b1∧c1−→ C?c2; D!c2; c3:= 5; f1:= 2*c3; F!f1 [] a1∧ ¬b1−→ G?c2; D!c2

[] else−→ skip ];

...

]

P3≡*[ A?a1, B?b1, C?c1;

[ a1∧b1∧c1−→ C?c2; D!c2; c3:= 5; f1:= 2*c3; F!f1 [] a1∧ ¬b1−→ G?c2; D!c2; c3:=c2

[] else−→ c3:=c1

];

X!a1, Y!c3

]

Figure 2.3: CHP selection statement and its transformation to DSA form.

N0G[f] = 0

Thus, a, b and c have all been assigned values prior to the selection statement G, but variablef has not. Any variable that appears in a guard condition of G must have a DSA index greater than 0 when the program enters the selection. Note that since the guard conditions cannot contain any assignments to variables, N0G[v] = N0Si[v] for every variable v and every command Si in the selectionG.

2.3.4.2 DSA Indices for Post-Selection Code

It now remains to ensure that the DSA indices chosen for the selection statement are consistent with those used for variables in code that follows the selection statement. Let us suppose that a variablev appears in multiple branches of the selection statement and is assigned to a different number of times in each one. Since each guarded command is converted independently to DSA form, the reaching definition of original variablev may be different at the end of different commands. For example,

in P2, the reaching definition of original variablec is now “c3 := 5” at the end of the first control branch, “G?c2” at the end of the second branch, and the pre-selection assignment “C?c1” at the end of the third. What new variable name—c1,c2 or c3—should be used to replace c in post-selection statements?

To determine the answer, we must either carry information about the guarded conditions outside of the selection statement, or join all of the different definitions ofv from the different commands back into a single variable definition for a single thread of control. (The terminology used here is deliberately similar to that of join nodes, or φ-nodes, used in static single-assignment compiler analysis [12]. The techniques used to merge multiple threads of control back into one are different, however.)

We choose the second option, and appoint the new DSA variable with the largest index to hold the definition that reaches post-selection statements. In other words, for a selection statement G with commandsSi,

NG[v] = max

i NSi[v] (2.1)

The DSA index ofv immediately following the selection statementG isNG[v], and all statements outside ofGwhose reaching definition ofvis inGshould usevNG

[v]on the RHS of their assignments.

Thus, the final DSA indices for variables in Gin P2 are

NG[c] = 3

NG[a] =NG[f] = 1

Note that since a is not assigned any values in G, N0G[a] = NG[a]. As it turns out, we can ignoreNG[f] since in our example’s original programP1,f is never used after the selection statement.

For the variables thatareused after the selection, we must ensure thatvNG

[v]is always defined at the end ofG! DDD accomplishes this using the following method:

1. For every command Si and every variablev assigned a value inG, if NSi[v] < NG[v] then

insert the copy statementvNG

[v] :=vNSi

[v] to the end of the command

2. For every statement S and every variable v where G ≺S and RD(v, S) is inG, change all instances ofv on the RHS ofS intovNG

[v]

The DSA indices inside selection statement G are now consistent with the selection’s surrounding code. When the above method is applied toP2, the resulting process isP3 in Figure 2.3.

2.3.4.3 Proof of Correctness

Theorem 3 Let G be a selection statement in a program P . If PPDSA and GGDSAas described above, then Gpgm≡ GDSA and GDSA is in DSA form.

Proof: The transformation in this section is limited to variable renaming and copy propagation at the end of guarded commands. It is therefore an operation-preserving transformation, and we use Theorem 1 to prove program equivalence by proving the preservation of effective reaching definitions fromP to PDSA.

Since only one commandSiinGis ever executed at a time, the application of the DSA straightline transformation to Si guarantees both that (Si)DSA is in DSA form and that Si

pgm≡ (Si)DSA by Theorem 2.

DDD replaces variablesvthat are used in guardsGi withvNG

0[v]. By Definition 8,N0G[v]is the reaching DSA index of v at the beginning ofG, soRD(v,Gi)≡RD(vNG

0[v],(Gi)DSA). Reaching definitions in the guards are therefore preserved. Since there are no assignments in guards, the replacement ofv withvNG

0[v]does not affect the DSA form of the selection.

It remains to consider any copy statements inserted at the end of Si. Any variable vNG

[v] on the LHS of these statements cannot have been defined before inSi (otherwise NSi ≥NG and the assignment would not exist), so this insertion leavesGDSA in DSA form. The new assignment kills the last definition ofv inSi, but is merely a copy statement that propagates the value of that def- inition, and thus leaves the effective reaching definition intact. The inserted assignments therefore do not change the semantics of the selection, andGDSA

pgm≡ G. 2