6.5 Phases
6.5.2 Transforming Final Constraints
So far, a main cost expression Eph and a set of non-final constraintsICph for a phaseph= (c1∨ · · · ∨cn)+ have been computed. Algorithm 3 completes the phase’s cost structure with a set of final constraints FCph(xsxf)(and possibly additional non-final constraints) that bind the intermediate variables ofEphand ICph. The algorithm receives the partial cost structure computed so far〈Eph,ICph〉, the final constraints of the CE’s cost structures〈FCc
1,FCc
2,· · ·,FCcn〉, and the cost equations of the phaseph.
Algorithm3Initialization
For each ci with cost structure〈Eci,ICci,FCci(x x0)〉, the algorithm maintains two sets ofpending con-straintsPsumsci andPmsci. These sets contain the constraints that have to be transformed to bind their corresponding Sum variables or Max/Min variables. FunctioninitPending (Line4) initializes the pend-ing sets uspend-ing the final constraints of eachci ∈phand considering the set of used variables in Eph and ICph. LetUsed:=uses(〈Eph,ICph〉), it returns the sets:
Psumsci:= P
iv./klk ∈FCci (smiv∩Used)6=; ∪
ivi ti ≤1,ivi ti≥1 #ci∈Used Pmsci:=
ivk≤ klk ∈FCci divek∈Used ∪
ivk≥ klk ∈FCci bivck∈Used
(1) Psumsci is initialized with the constraints of FCci such that some smivk in smiv has to be bound (because it is used in the cost structure). Additionally, the constraints ivi ti ≤1 andivi ti ≥1 are added if #ci has to be bound. The variable ivi ti represents the number of times ci is evaluated such that smivi ti =#ci.
(2) Pmsci is initialized with the constraints of FCci with a single variable on the left side such that the corresponding dive or bivc has to be bound (it is used in the cost structure). As mentioned before, the constraints of the formPm
k=1ivk≤ klk can be split into ivk ≤ klk for1≤k ≤m on demand during the initialization ofPmsci.
Algorithm3Main loop
The main loop of the algorithm iterates over the pending sets (Lines 7-9). In each iteration, the algorithm removes one constraint fc from one of the pending sets (function takeElem in Line 8) and applies one or several strategies to the removed constraint (function applyStrategies in Line 9). The variableorigin indicates the origin of the constraint, that is, whether the constraint was in a setPsumsci orPmsci and from which CEci. A strategy generates new constraints (final or non-final) for the phase’s cost structure using the cost equations in ph. Psums is also passed to applyStrategies because some strategies can take additional pending constraints into account. The generated constraints are added to
6.5. Phases 87
the setsICph orFCph. A strategy can also add additional constraints to the pending setsPsumsorPms to be processed later. The algorithm repeats the process until allPsumsci andPmsci are empty (Line7).
In principle, the algorithm can finish without generating constraints for all intermediate variables.
For instance, if the cost of the phase is actually infinite or some of the strategies fail to generate any constraint. It is also possible that the algorithm does not terminate if new constraints keep being added to the pending sets indefinitely. However, this does not happen often in practice. The loop condition can be strengthened to ensure termination. For instance, the loop can exit if all the intermediate variables are already directly or indirectly bound by final constraints. In addition, a limit can be established on the total number of iterations or on the number of times that additional pending constraints can be added toPsumsandPms.
The remaining of this section contains a series of strategies to deal with different kind of constraints and phase behaviors and, at the end of the section, there is an example of how these strategies work together to obtain a complex cost structure.
Inductive Sum Strategy LetP
iv./kl(x x0)k ∈Psumsci be a pending constraint, the strategy tries to find a linear expression that approximates the sumP#ci
j=1kl(ac
ija0c
ij)kfor any evaluation in terms of the initial and final values of the phase (asaf).
Remember that the CEs in the phase have the formci:C(x :y) =ϕi,b,C(x0:y0),bwhereC(x0:y0)is the only recursive call;ϕi is the CE’s constraint set; andx y and x0y0 are the input and output variables of the head and the recursive call.
Let us consider first the simple case where ci is the only CE in the phase. The strategy uses the CE’s constraint set ϕi and Farkas’ Lemma to generate a candidate linear expression cd(x) such that ϕi⇒(kl(x x0)k./cd(x)−cd(x0)≥0). If a candidatecd(x)is found, for any evaluation we have:
Xsmiv./
#ci
X
j=1
kl(acija0c
ij)k./
#ci
X
j=1
(cd(acij)−cd(a0c
ij)) =cd(as)−cd(af)
This is because each intermediate −cd(a0c
ij) and cd(acij+1) cancel each other (cd(a0c
ij) = cd(acij+1)).
Therefore, the constraintP
smiv./kcd(xs)−cd(xf)kis valid and can be added toFCph.
Example 6.26. This is the case of phase(9)+ of Program13with the variablesis,zs,if,zf andPsums9= {ivi t
9≤1,ivi t
9≥1}. The strategy generates the candidate−ifor both ivi t
9 ≤1andivi t
9 ≥1. We have {i<z,i0 = i+1} ⇒ k1k ≤ −i−(−i0) and {i <z,i0 =i+1} ⇒ k1k ≥ −i−(−i0). The generated final constraints aresmivi t9 ≤ kif −isk andsmivi t9 ≥ kif −isk. Later kif −isk will become kz−ik in chain (9)+(8)andkzkin CE7. The variablesmivi t
9corresponds toiv1 in Figure6.3.
If the phase contains other CEs ce (e 6= i), their effect on the sum has to be taken into account. For example, suppose that we have anotherce(e6=i) that increments our candidate by two (ϕe⇒cd(x0) = cd(x)+2). Between two consecutive evaluations ofci(thej-th and the(j+1)-th evaluations), there might be a number of evaluations ne of ce that increment the candidate by 2ne (cd(xc
ij+1) =cd(xc0
ij) +2ne).
If we consider the complete phase evaluation wherece is evaluated#ce times, we obtain the following sum:
Xsmiv./
#ci
X
j=1
kl(acija0c
ij)k./
#ci
X
j=1
cd(acij)−cd(a0c
ij) =cd(as)−cd(af) +2#ce
Table 6.1.: CE Classification conditions for strategies: A CE ce can be classified into a class with respect to a candidatecd(x)if its condition is satisfied. Each class defines an expression/intermediate variable.
Class Condition when./is≤ Condition when./is≥ Defines
Cnt (P
iv0./kl0k)∈Psumsce∧ kl0k./cd(x)−cd(x0)≥0 cnte:=P smiv0 Dc 0≤dce(x x0)≤cd(x)−cd(x0) dce(x x0)≥cd(x)−cd(x0) ivdce :=kdce(x x0)k Ic ice(x x0)≥cd(x0)−cd(x) 0≤ice(x x0)≤cd(x0)−cd(x) ivice:=kice(x x0)k
CntR (P
iv0≤ kl0k)∈Psumsce∧ kl0k ≤ kcd(x)k − kcd(x0)k cntre:=P smiv0 Rst cd(x0)./krste(x)k ivrste :=krste(x)k
CntT P
iv0./kl0k
∈Psumsce∧l0./cd(x)≥0∧cd(x0)−cd(x)./qe cntte:=P smiv0
Nop cd(x0)−cd(x) =0
That is, the sum computed for the simple casecd(as)−cd(af)plus the sum of all the increments to the candidate2#ceeffected by CEce. This constraint can be expressed with the following intermediate and final constraints:
Psmiv./ivcd+−ivcd−+2#ce ivcd+./kcd(xs)−cd(xf)k ivcd−!./k−cd(xs) +cd(xf)k whereivcd+andivcd−represent the positive and negative part ofcd(xs)−cd(xf)and!./is the opposite operator of./(if./is≤, the operator!./is≥and vice-versa).
In the general case, the strategy is divided into three steps:
1. First, the strategy generates a candidate cdusing ci’s constraint setϕi, the condition kl(x x0)k ./
cd(x)−cd(x0)≥0, and Farkas’ Lemma (as in the simple case before).
2. Next, it classifies the CEs of the phase ce ∈ ph (including ci) according to their effect on the candidate.
3. Finally, it uses this classification to generate constraints that take these effects into account.
Let us consider now the second and third steps:
Cost Equation Classification
Each ce∈phhas to be classified with respect to a candidatecdinto a class. Each class has a condition and defines a linear expression (see Table 6.1). In order to classify a CE ce into a class, its condition has to be implied by the corresponding CE’s constraint setϕe. Some of the conditions contain unknown linear expressions: dce(x x0), ice(x x0) or rste(x x0) (For the classes Dc, Ic and Rst respectively). The implication can be verified and the unknown linear expressions can be inferred using Farkas’ Lemma and linear templates. The considered classes in this strategy are6:
• Cnt:ce∈Cntif there is a constraintP
iv0./kl0k ∈Psumscethat can also be bound by the candidate:
ϕe ⇒ kl0k./ cd(x)−cd(x0). The expressionP
smiv0 can be incorporated to the left-hand side of the constraint. We definecnte:=P
smiv0 as a shorthand. Note thatci, whose constraint was used to generate the candidate in the first place, trivially satisfies the condition and thusci ∈Cnt.
6 The rest of the classes are used by other strategies.
6.5. Phases 89
• Dc: ce∈Dcif in each evaluation ofcethe candidate is decremented by at leastdce(x x0)(or at most dce(x x0) if./ is ≥). A fresh intermediate variable is assigned to this amountivdce :=kdce(x x0)k. To generate a valid constraint, the sum of all those decrements, that issmivdce, is subtracted from the right-hand side of the constraint.
• Ic: ce ∈ Ic if in each evaluation of ce the candidate is incremented by at most ice(x x0) (or at least ice(x x0) if ./ is ≥). As before, a fresh intermediate variable is assigned to that amount ivice :=kice(x x0)k. To generate a valid constraint, the sum of all those increments, that is smivice, is added to the right-hand side of the constraint.
Constraint Generation
Once all the CEs in a phase have been classified with respect to a candidate, the strategy generates the following constraints to be added to the phase’s constraints:
Theorem 6.27. Let!./be the reverse of./(e.g.≥if./is≤). If everyce∈phhas been successfully classified intoCnt,IcorDcwith respect to a candidatecd(x), the following constraints are valid:
X
ce∈Cnt
cnte./ivcd+−ivcd−+X
ce∈Ic
smivice− X
ce∈Dc
smivdce ivcd+./kcd(xs)−cd(xf)k ivcd−!./k−cd(xs) +cd(xf)k
The proof of this theorem can be found in Section 6.10. Note that ivcd+ and −ivcd− represent the positive and negative part ofcd(xs)−cd(xf). The constraints bind the sum of allsmivincnte (for each ce∈Cnt) tocd(xs)−cd(xf)plus all the incrementsP
ce∈Icsmivice minus all the decrementsP
ce∈Dcsmivdce. If the classIcis empty,cd(xs)−cd(xf)is guaranteed to be positive (the candidate is never incremented) and the summand−ivcd−and its corresponding constraintivcd−!./k−cd(xs)+cd(xf)kcan be eliminated.
Finally, the strategy adds constraints for the new intermediate variables ivice and ivdce to the corre-sponding pending setPsumsce so their sumssmivice andsmivdce are bound afterwards:
• For eachce∈Ic, the constraintivice ./kice(x x0)kis added toPsumsce
• For eachce∈Dc, the constraintivdce!./kdce(x x0)kis added toPsumsce
Furthermore, if ./ is ≤ the first constraint in Theorem 6.27 can be simplified by removing the in-termediate variables that are subtracted. This maintains the validity of the constraint (remember that intermediate variables are always non-negative) which has the following form:
X
ce∈Cnt
cnte≤ivcd++X
ce∈Ic
smivice
This simplification also implies that the pending constraints for eachce∈Dccan be discarded.
Example 6.28. In phase(3.1∨3.2)+we haveiv5≤ ky−y0+1k ∈Psums3.2. A valid candidate is y+x. The CEs are classified as follows: CE 3.2∈ Cntbecause it has generated the candidate andcnt3.2 :=smiv5; and CE3.1∈Dcbecause y+x decreases in CE3.1bydc3.1=0. The generated constraints are: smiv5≤ ivcd+−ivcd−−smivdc, ivcd+≤ k(ys+xs)−(yf +xf)kandivcd+≤ k−(ys+xs)+(yf +xf)k. However, given thatIcis empty anddc3.1=0, they can be simplified to a single constraint:smiv5≤ k(ys+xs)−(yf+xf)k (wheresmiv5 isiv6 in Figure6.3).
Example 6.29. The class Cnt makes possible to bind Sum variables of differentci under a single con-straint. For instance, if we had7 ivi t3.1 ≥1∈Psums3.1 andivi t3.2 ≥1∈Psums3.2, the expression x would be a valid candidate with the classificationCnt={3.1,3.2}withcnt3.1:=smivi t3.1 andcnt3.2:=smivi t3.2.
7 smivi t3.1 andsmivi t3.2 are actually not needed for computing the cost of the program in this case. Therefore, these constraints are never added to the pending sets.
Program 14 Cost relations void cds (int x ,int y){
int z =*;
while(x >0 && y >0 && z >0) {
if(*) y - -;
else y =*;
x - -;
z - -;
tick (1) ; }
1.1:cds(x,y) ={x>0,y>0,z>0},wh[(3∨4)+(2)](x,y,z) 1.2:cds(x,y) =wh[(2)](x,y,z)
2:wh(x,y,z) ={}
3:wh(x,y,z) ={x>0,y>0,z>0,x0=x−1,y0= y−1, z0=z−1}, 1,wh(x0,y0,z0)
4:wh(x,y,z) ={x>0,y>0,z>0,x0=x−1,z0=z−1}, 1,wh(x0,y0,z0)
Figure 6.4.:Program14: Example where multiple candidates are important
The strategy would generate the (simplified) constraintsmivi t3.1+smivi t3.2 ≥ kxs−xfkwhich is equiva-lent to#c3.1+#c3.2≥ kxs−xfkand represents thatwh3iterates at leastkxs−xfktimes. WithoutCnt, the strategy would fail to obtain a non-trivial lower bound for #c3.1 or #c3.2 as they can both be 0(if considered individually).
Discussion
It is worth noting that while the initial motivation for this strategy was to find a linear expression that could bind the sumP
smiv, the resulting constraints do not necessarily represent a linear expression due to the effects of other CEs in the phase. For instance, the added variables capturing incrementssmivice can represent non-linear expressions.
The strategy can fail at two points. (1) It can fail to generate a candidate (if there is no linear constraint that satisfies the conditions) or (2) it can fail to classify the CEs of a phase with respect to a given candidate. In the latter case, it can attempt to generate a different candidate (Possibly taking the CEs where the classification failed into account). Moreover, even if the strategy does not fail, we can adjust it to consider several candidates. In general, the more candidates it considers, the more constraints it will be able to generate and the better precision it can achieve. Evidently, this comes at a cost in terms of performance.
Example 6.30. Let us compute a cost structure for the phase(3∨4)+ of Program14(Figure6.4). The initial pending set of CE 3 is Psums3 = {ivi t
3 ≤ 1,ivi t
3 ≥ 1}. The expression y is a valid candidate for ivi t
3 ≤ 1 in CE3. However, CE 4 cannot be classified successfully with respect to the candidate y and consequently the strategy fails at this point. The strategy can backtrack and consider a different candidate.
Both x and z are valid candidates and can successfully classify CE 4 in Cnt with cnt4 = ivi t4. The generated constraints are: #c3+#c4≤ kxs−xfkand#c3+#c4≤ kzs−zfkwhich can be transformed to
#c3+#c4≤ kxkand#c3+#c4≤ kzkfor the chain(3∨4)+(2). Unfortunately, even though the candidate z was successfully classified, later in the analysis its corresponding constraint #c3+#c4 ≤ kzk will be lost. This is becausezis initialized to an unknown value and thus it cannot be expressed in terms of the input parameters of function x and y. Therefore, if the analysis considers only the candidate z, it will fail to obtain a bound for CE1.1.
Finally, note that this strategy is completely symmetric. It can be used to obtain upper and lower bounds. However, it is not useful for divergent phases in which the variablesxf cannot be related to any specific value or output variable.
6.5. Phases 91
Inductive Sum Strategy with Resets
This variant of theInductive Sumstrategy allows us to obtain upper bounds of sums in terms of the initial variables of the phase only (xs). Such bounds are also useful for divergent phases. The strategy follows the same scheme as before.
Candidate Generation LetP
iv≤ klk ∈Psumsci, it generates a candidatecd(x)using the constraint setϕi of CEci and Farkas’
Lemma. However, this time the strategy uses the conditionkl0k ≤ kcd(x)k − kcd(x0)k. This condition considers only the positive part of the candidate which allows us to ignore the final value of the candidate cd(xf)and guarantee that the generated constraint is valid for (selected partial evaluations of) divergent phases (see Definition6.3).
Cost Equation Classification
As in the previous strategy, the CEs in the phase are classified. This strategy considers the classCntR, instead ofCnt, whose condition considers the positive part of the candidates as well (see Table6.1). It considers the classesDcandIcto capture decrements and increments of the candidate and, in addition, it considers theRstclass to support phases where the candidate is reset to a completely different value.
Ifce∈Rstthe candidate is reset to a value of at mostkrste(x)k. A fresh intermediate variable is assigned to such reset valueivrste :=krste(x)k.
Constraint Generation
Theorem 6.31. If everyce∈phis classified intoCntR,Ic,DcandRstwith respect to a candidatecd(x), the following constraints are valid:
X
ce∈CntR
cntre≤ivcd+X
ce∈Ic
smivice+ X
ce∈Rst
smivrste ivcd ≤ kcd(xs)k
The strategy generates the following pending constraints:
• For eachce∈Ic, the constraintivice ≤ kic(x x0)kis added toPsumsce.
• For eachce∈Rst, the constraintivrste≤ krst(x)kis added toPsumsce.
Note that this strategy ignores the decrements of ce ∈Dc. For divergent phases the lower bounds on variablessmivdce always be0, because the evaluation can be truncated at any point.
Basic Product Strategy
In many cases, the previous strategies fail to even infer a candidate. Given a constraintP
iv./kl(x x0)k ∈ Psumsci, it might be impossible to infer a linear expression representingP#ci
j=1kl(xjx0j)k. This is the case for most nested loops such aswh10in Program13.
Example 6.32. Consider the cost computation of phase(7)+ of Program 13 in which the pending set Psums7 contains the constraintiv1 ≤ kzk. In the phase, the variable z does not change in CE7 and#c7 is at most y so P#c7
j=1kzk =kyk · kzk which is non-linear. This result can be obtained by rewriting the constraintiv1≤ kzkasiv1≤1·kzkand generating the constraintsmiv1≤smivi t
7·divemz(that corresponds toiv2 ≤iv3·iv4 using the intermediate variables of Figure6.3). Then,ivi t
7 ≤1is added toPsums7 and ivmz ≤ kzk is added to Pms7. These constraints will be later processed by the strategies Inductive Sum andMax-Minrespectively.
In general, given a constraintP
iv≤ klk ∈Psumsciwherelis not a constant, theBasic Productstrategy generates the non-final constraint:
Xsmiv≤smivi ti· divep
And it adds the pending constraintsivi ti ≤ 1 to Psumsci andivp ≤ klk to Pmsci. This way, it reduces a complex sum into a simpler sum and a max/minimization. This strategy is the main source of non-final constraints in the form of a product of two intermediate variables. It proceeds analogously for constraints with the operator≥.
Max-Min Strategy
This strategy deals with constraints iv./klk ∈Pmsci and its role is to generate constraints for Maxdive and Minbivcvariables. It follows the same three steps scheme or theInductive Sumstrategies.
First, it generates a candidate cd(x) using the CE’s constraint set ϕi. However, the condition used to generate the candidate is simply l ./ cd(x) since it has to bind a single instance of l instead of the sum of all its instances. Second, the strategy classifies thece ∈phwith respect to the candidate. In this case, it does not consider the classCntbut it considersIc,DcandRst(see Table6.1). Third, the strategy generates constraints:
Theorem 6.33. Letiv≤ klk ∈Pmsciand letcd(x)be a candidate such thatϕi⇒l≤cd(x). If everyce∈ph is classified intoDc,IcandRstwith respect tocd(x), the following constraints are valid:
dive ≤ivma x +X
ce∈Ic
smivice ivma x ≤max
ce∈Rst(diverste,ivcd) ivcd≤ kcd(xs)k
These constraints bind diveto the sum of all the incrementssmivice force∈Icplus the maximum of all the maximum values that the resets can takediverste. This maximum also includes the initial value of the candidatecd(xs) in case it is never reset.
The strategy adds the following pending constraints:
• For eachce∈Ic, the constraintivice ≤ kice(x x0)kis added toPsumsce.
• For eachce∈Rst, the constraintivrste ≤ krste(x)kis added toPmsce.
The strategy proceeds analogously for constraints with the operator≥ but it subtracts the decrements instead of adding the increments and takes the minimum of the resetsbivcrste:
Theorem 6.34. Letiv≥ klk ∈Pmsciand letcd(x)be a candidate such thatϕi⇒l≥cd(x). If everyce∈ph is classified intoDc,IcandRstwith respect tocd(x), the following constraints are valid:
bivc ≥ivmin− X
ce∈Dc
smivdce ivmin≥ min
ce∈Rst(bivcrste,ivcd) ivcd ≥ kcd(xs)k
Example 6.35. In Example6.32the constraintivmz≤ kzkwas added toPms7 during the computation of the cost of(7)+. TheMax-Minstrategy generates a candidate z and classifies CE7 inDcwith dc7 :=0 (z is not modified in CE7). The resulting (simplified) constraint is divemz≤ kzsk(which corresponds to iv4≤ kzskusing the intermediate variables of Figure6.3).
Triangular Sum Strategy
This strategy represents an alternative to theBasic Productstrategy for dealing with constraintsP iv./
kl(x x0)k ∈Psumsci wherekl(x x0)kvaries in each iteration by a constant amount.
6.5. Phases 93
Refined cost relations of Program2
2:for2(x,n) ={y=x,x <n,x0=x+1},for3[(3)+(4)](y,n),for2(x0,n) 3:for2(x,n) ={x≥n}
4:for3(y,n) ={y <n,y0=y+1}, 1,for3(y0,n) 5:for3(y,n) ={y ≥n}
Lower bound: knk2/2+knk/2
Figure 6.5.:Refined cost relations of Program2and its lower bound
Example 6.36. A typical example is Program2(in Page5). Figure6.5contains its refined cost relations.
In this example, cost equations2and3represent the outer loop and4 and5 the inner loop. The chain that represents the total cost of the program is(2)+(3). The final values of the variables xo, yo and no are not included in the CRs to simplify the presentation. Let us consider obtaining the lower bound of such an example.
Assume the cost of the inner loop (chain (4)+(5)) is 〈iv1,;,{iv1 = kn− yk}〉 which yields a cost of
〈iv1,;,{iv1 =kn−xk}〉for CE2. The main cost expression of the phase(2)+ is E(2)+ :=smiv1, there are no non-final constraints and the pending sets are:Psums2={iv1≤ kn−xk,iv1≥ kn−xk}andPms2=;.
If we apply the Basic Product strategy toiv1≥ kn−xk, we would obtainsmiv1≥smivi t
2· bivc2and later smivi t
2≥ kns−xskandbivc2≥1(the minimum value ofkn−xkis1in the last iteration) which represents the imprecise lower boundkn−xk ·1.
Instead, we consider thatkn−xkdecreases by at most1in each iteration so we can reformulate:
#c2
P
j=1
knj−xjk ≥
#c2
P
j=1
(kns−xsk −(j−1)) =kns−xsk ·#c2−
#c2−1
P
j=0
j=kns−xsk ·#c2−12(#c22−#c2) This expression can be represented with constraints as follows:
smiv1≥ivp1−12ivp2+12smivi t2 ivp1≥ivini·smivi t2 ivp2≤smivi t
2·smivi t
2 ivini≥ kns−xsk
Note that the constraint over ivp2 has ≤ instead of≥. This is because ivp2 appears negated in the first constraint and it has to be maximized. Later, applying theInductive Sumstrategy toivi t
2≤1andivi t
2≥1 (inPsums2), we generatesmivi t2 =k(ns−xs)−(nf −xf)k. When we compute the cost of the complete chain(2)+(3), we transformk(ns−xs)−(nf −xf)k intokns−xsk(because nf −xf must be0in chain (2)+(3)). If we minimize the cost of the resulting cost structure, we obtain:
kns−xsk2−12kns−xsk2+12kns−xsk =12kns−xsk2+12kns−xsk= 12knsk2+12knsk In the general case, given a constraintP
iv./klk ∈Psumsci, the strategy generates a candidatecd(x) under the condition that it approximates the cost of one instance ofkl(x x0)k, it is positive, and it varies by a constant amountqi ∈Q. The condition is:
l./cd(x)≥0 ∧ cd(x0)−cd(x)./qi
This strategy considers the classesCntT andNop(see Table6.1in Page 89). Ifce∈CntT, there exists a constraint(P
iv0./kl0k)∈Psumscethat is bound by the candidate and the candidate varies by an amount qe. As in previous strategies, this condition coincides with the one used to generate the candidate and ci∈CntT. The CEsce∈Nopdo not modify the candidate.
Program 15 Refined cost relations
1while (x >0) {
2 if (*) {
3 while (y >0 && *) {
4 y - -;
5 tick (2) ;
6 }
7 } else {
8 if (*) y ++;
9 else y=z;
10 }
11 x - -;
12}
1:wh1(x,y,z) ={x≤0}
2:wh1(x,y,z) ={x>0,y> y0≥0,x0=x−1},wh3[(7)+(6)](y: y0), wh1(x,y0,z)
3:wh1(x,y,z) ={x>0,y= y0≥0,x0=x−1},wh3[(6)](y: y0), wh1(x,y0,z)
4:wh1(x,y,z) ={x0=x−1≥0,y0= y+1},wh1(x0,y0,z) 5:wh1(x,y,z) ={x>0,x0=x−1,y=z},wh1(x0,y0,z) 6:wh3(y: yo) ={y =yo}
7:wh3(y: yo) ={y ≥1,y0= y−1}, 2,wh3(y0: yo)
Figure 6.6.:Program15: Example with complex phase
Theorem 6.37. If everyce∈phis classified intoCntTandNopwith respect to a candidatecd(x). Letq be q:= max
ce∈CntT(qe)when./is≤orq:= min
ce∈CntT(qe)when./is≥. The following constraints are valid:
P
ce∈CntT
cntte./ivp1+2qivp2−q2ivits , ivp1./ivini·ivits ivp2=ivits·ivits, ivits= P
ce∈CntT
smivi te, ivini./kcd(xs)k
The intermediate variables ivp1,ivp2,ivits, and ivini are fresh: ivp1 andivp2 simply represent different parts of the expression, the variable ivits represents the sum of all the iterations#ce of ce ∈ CntT, and ivini represents the initial value of the candidate. The constraints of the formiv=x stand foriv≤x and iv≥x. Finally, for eachce∈CntT, the constraintsivi te ≤1andivi te≥1are added to eachPsumsce.
This strategy allows us to obtain both upper and lower bounds that are more precise than the ones ob-tained with the Basic Product strategy. However, it also generates many more constraints so its adequacy highly depends on the goals of the analysis. For instance, if we are only interested in the asymptotic complexity, this strategy can be advantageous for lower bounds but not for upper bounds. Consider Program 2, the expression knk22 + knk2 is the upper and lower bound obtained with the Triangular Sum strategy and it is quadratic. Without the Triangular Sum strategy, the obtained upper and lower bounds aren2 andnrespectively. While both are imprecise at the concrete level, the upper bound is asymptot-ically precise. In the future work chapter (Chapter10) there is a discussion on how this strategy could be improved and extended.