• Keine Ergebnisse gefunden

Control-flow Refinement of a Cost Relation

5.5 Extension to Multiple Recursion

5.5.2 Control-flow Refinement of a Cost Relation

The call relation is generalized for CEs with several recursive calls. A CEccallsd, writtencd, if the joint constraints ofcandd are satisfiable. The relation does not distinguish the position in whichd can be called.

Definition 5.50(CE Call Relation). LetCbe a cost relation and letc,dC, we havecdif and only if givenc:C(x : y) =ϕ0,b0,C(x1 : y1),ϕ1,b1,C(x2 : y2),· · ·,C(xn: yn),ϕn,bn andd:C(x : y) =ϕ00,· · ·, there is aksuch thatVk

i=0ϕiϕ00[x y/xiyi]is satisfiable.

As in the linear case, phases also correspond to SCCs of the call-graph but if a SCC contains CEs with more than one recursive call, a different kind of phase is generated.

Definition 5.51(Multiple Phase). LetSbe the vertices of a SCC inG(C). IfS={c1,· · ·,cm}is recursive, and there is some ci with more than one recursive call, it generates an iterative multiple phase ph := M(c1∨ · · · ∨cm)+/ω. In this case, there is no distinction between iterative and divergent phases at this point. A phaseM(c1∨ · · · ∨cm)+/ω represents an evaluation in which every branch of the tree matches the pattern(c1∨ · · · ∨cm)+ or(c1∨ · · · ∨cm)ω.

If S = {c} cannot iterate (c6→ c) and has more than one recursive call, it generates a non-iterative multiple phaseph:=M(c)in whichcis evaluated once.

1:append(l1,l2:l) ={l1=0,l=l2}

2:append(l1,l2:l) ={l1≥1,l1=l10+1,l=l0+1}, 1,append(l10,l2:l0) 3:subtr(t :l) ={t =0=l}

4:subtr(t :l) ={t =1+t1+t2,t1≥0,t2≥0,l=l3+1}, 1,subtr(t1:l1),subtr(t2:l2), {l1≥1,l1+l2=l3},append[(2)+(1)](l1,l2:l3)

5:subtr(t :l) ={t =1+t1+t2,t1≥0,t2≥0,l=l3+1}, 1,subtr(t1:l1),subtr(t2:l2), {l1=0,l2=l3},append[(1)](l1,l2:l3)

Figure 5.4.:Cost Relations of Program4: Partially refined multiple recursion

Whereas a simple phase corresponds to a sequence of CE evaluations, a multiple phase represents a tree. In an evaluation, a simple phase can only be followed by another phase but a multiple phase can be followed by several other phases as each leaf of the tree can continue with calls to different phases.

The call relation→is extended for multiple phases similarly as with simple phases. Letph1andph2be phases generated from SCCsS1 andS2. We haveph1ph2 if there isc1ph1 andc2ph2andc1c2 andph16= (c1∨ · · · ∨cm)ω. Note that a multiple phase can call other phases even if some of its branches might diverge. Once the call relation has been lifted to phases, the notion of chain is extended taking into account multiple phases.

Definition 5.52(Chain). Achainis atreeof phases defined recursively:

ch:= ph ifph= (c)andcis not recursive, orph= (c1∨ · · · ∨cm)ω

|ph·ch0 ifphis not multiple,ch0=ph0_ andphph0

|ph·CH phis multiple, andCH:={ch0|ch0=ph0_∧phph0} Note that in the third caseph·CH, the phasephmight have a branch that diverges.

Example 5.53. Figure5.4contains the cost relations of Program4(in Page11) where the CEs ofsubtr are partially refined. Note for example that the constraint sets before the call toappend constitute the summaries of the chains inappend. The chains of CRsubtrareM(4∨5)+/ω{(3)}and(3).

Chain Evaluations

The definitions of CR-paths (Definition5.7) and chain evaluations (Definition 5.8) do not need to be adapted. The only difference is in the definition of the language Lch that paths have to match. The language of a chainch=ph·CH)that starts with a multiple phaseph=M(c1∨ · · · ∨cn)+/ωis:

Lch:=L(c1∨···∨cn)ω∨ L(c1∨···∨cn)+· _

ch∈CH

Lch

A CR-path in chcan be infinite over the CEs of phor it can have a finite prefix that matches phand a suffix that matches the language of one of the chains inCH.

Theorem 5.9(Chain Refinement Completeness) is also valid. Below the proof is adapted to the case of multiple recursion.

Proof of Theorem5.9for Multiple Recursion. The same reasoning as in the original proof can be applied to show that given an evaluation T, for every ppathsC(T)there is a chainchsuch thatlabel(p)∈ Lch. However, in the case of a cost relation with multiple recursion, the number of CR-paths inT can be more than one. Therefore, we have to prove that given two different pathsp1,p2pathsC(T), they match the same chain. That is, if p1∈ Lchand p2∈ Lch0, thench=ch0.

The paths p1 and p2 have a non-empty prefix in common because they both start at the root of the evaluation. Let b be the maximal common prefix, that is, p1 = b·p01 and p2 = b·p20. We have that

5.5. Extension to Multiple Recursion 69

p10 6= p02 so there must be a node in b = b1·c(a : bb2 whose CEchas multiple recursion, otherwise p10 and p02 cannot differ. A CE with multiple recursion generates only one multiple phase phand there is only one chain that starts with such a phaseph·CH. This is becauseCHis uniquely determined (see Definition5.52). Therefore, the pathsc(a:b)·b2·p10 andc(a:b)·b2·p20 belong to the same chainph·CH.

Finally, the rest of the path b1 is common top1 andp2 so ifp1∈ Lchthenp2∈ Lchand vice-versa.

Discarding Divergent Phases

The evaluation of a multiple phase M(c1∨ · · · ∨cm)+/ωmight have one branch that follows the pattern (c1∨· · ·∨cm)ω. If we prove such a pattern is unfeasible, we can transform the phase intoM(c1∨· · ·∨cm)+. This can be done similarly to how it is done for divergent phases. That is, a multi-path linear constraint loop is extracted from the phase. If that loop, denoted phase loop, is proved terminating, the divergent pattern(c1∨· · ·∨cm)ωis unfeasible. The only difference is that a CE in a multiple phase can have several recursive calls and consequently generate multiple paths. One path for each recursive call.

Theorem 5.54. If the phase loop of a phase ph = M(c1∨ · · · ∨cm)+/ω is terminating, then the phase is equivalent toph0=M(c1∨ · · · ∨cm)+. That is, letch=ph·CHandch0=ph0·CHbe chains that start with phandph0, their evaluations coincide¹CRS|C[ch]º=¹CRS|C[ch0.

Proof. We prove that if the phase loop ofphis terminating, then¹CRS|C[ch]º=¹CRS|C[ch0]º. It is clear that¹CRS|C[ch0]º⊆¹CRS|C[ch]º. Assume ¹CRS|C[ch]º6⊆¹CRS|C[ch0]º, that is, there is an evaluation T ∈ ¹CRS|C[ch]º such that T 6∈ ¹CRS|C[ch0]º. The evaluation T has to have a path ppathsC(T) such thatlabel(p)∈ L(c1∨···∨cm)ω. Such a path has to be infinite and formed only by nodes ofc1,· · ·,cm. However, if the phase loop ofphis terminating, such an infinite path cannot exist. Consequently, there is no evaluation T such that T ∈ ¹CRS|C[ch]º and T 6∈ ¹CRS|C[ch0]º and therefore ¹CRS|C[ch]º ⊆

¹CRS|C[ch0]º.

Example 5.55. Continuing with Example5.53, consider phaseM(4∨5)+/ωof Program4. CE4generates two loop paths, one for each recursive call. The loop paths are obtained by projecting its first constraint set{t =1+t1+t2,t1≥0,t2≥0,l =l3+1} onto t,t1and t,t2, which generate{t ≥1+t1,t1≥0}

and{t ≥1+t2,t2≥0}. Then, we rename the variables of the recursive calls to primed versions of the head variables and obtain{t ≥1+t0,t0≥0}in both cases. CE 5generates another two paths that are equal. Finally, the expressiont is a valid ranking function of{t≥1+t0,t0≥0}andM(4∨5)+/ωcan be simplified toM(4∨5)+.

Refined Cost Equations

The CE Case Distinction is a generalization of the linear case. A CE with multiple recursive calls generates more than one partial CE, one per recursive call.

Definition 5.56(Multiple Recursive CE Case Distinction). Let a cost equationc:C(x :y) =ϕ0,b0,C(x1: y1),ϕ1,b1,C(x2: y2),· · ·,C(xn: yn),ϕn,bnwithnrecursive calls, the case distinction generatesnpartial CEscpj for1≤ jn:

cpi:C(x :y) = j−1V

i=0

ϕi,b0,C(x1:y1),· · ·,C(xi: yi) and a complete CEcc or a tail-divergent CEctd:

ctd:C(x : y) = Vn

i=0

ϕi,b0,C(x1:y1),b1,C(x2:y2),· · ·,C(xn:yn),bn ifcis tail-divergent cc:C(x :y) = Vn

i=0

ϕi,b0,C(x1: y1),b1,C(x2: y2),· · ·,C(xn: yn),bn otherwise

Theorem 5.57. The multiple recursive CE case distinction is sound and precise.

Proof. The proof is analogous to the one of Theorem5.15. The function CEspcan be generalized to map eachctocc,ctd, or somecpi.

Example 5.58. Consider CE4of Program4(Figure5.4):

4:subtr(t:l) = {t =1+t1+t2,t1≥0,t2≥0,l=l3+1}, 1,subtr(t1:l1),subtr(t2:l2), {l1≥1,l1+l2=l3},append[(2)+(1)](l1,l2:l3)

Its complete and partial versions are:

4c:subtr(t :l) = {t =1+t1+t2,t1≥0,t2≥0,l=l3+1,l1≥1,l1+l2=l3}, 1, subtr(t1:l1),subtr(t2:l2),append[(2)+(1)](l1,l2:l3)

4p1:subtr(t:l) = {t =1+t1+t2,t1≥0,t2≥0,l=l3+1}, 1,subtr(t1:l1)

4p2:subtr(t:l) = {t =1+t1+t2,t1≥0,t2≥0,l=l3+1}, 1,subtr(t1:l1),subtr(t2:l2) Refined Chains

Similarly, the phase case distinction and the refined chains are extended for multiple phases. For a multiple phase, only two phases are generated, a complete phase and a divergent phase.

Definition 5.59 (Multiple Phase Case Distinction). Let ph= M(c1∨ · · · ∨cm)+/ω be a multiple phase, such thatc1,· · ·,ck are tail-divergent and eachci has #calls(ci) recursive calls, the following phases are generated:

1. A complete phasephc:=M(cck+1∨ · · · ∨ccm)+ where the phase is completely evaluated.

2. A divergent phase

phd :=M

‚ k _

i=1

ctdi

#calls(ci)

_

j=1

cpij

m

_

i=k+1

cci

#calls(ci)

_

j=1

cipj

Œ+/ω

where the evaluation diverges. Basically, the divergent phase contains all the refined CEs of the original phase. If non-termination has been discarded within the phase, then phd has the super-script+instead of+/ω.

The non-iterative case is again a particular case. Let ph= M(c) a phasephd := (cpj) is generated for eachcpj. Ifcis tail-divergentphd := (ctd)is generated, otherwisephc:= (cc)is generated.

Note that for multiple phases, no distinction is made between divergent and tail-divergent. Once the case distinction for multiple phases has been defined, the refined chains (Definition 5.21) can be extended to take these phases into account.

5.5. Extension to Multiple Recursion 71

Definition 5.60 (Refined Chains). The definitions of the functions term and nterm are extended to support chains with multiple phases:

term(ch):=

phc ifch=ph

phc·chc0 ifch=ph·ch0andchc0=term(ch0)

phc·CH0 ifch=ph·CHandCH0:={term(ch0) |ch0CH}

nterm(ch):=









{phd} ifch=phphis divergent

{phtd} ifch=phph= (c) {phtd·chc0 |chc0=term(ch0)}

∪{php·chω0|chω0nterm(ch0)}ifch=ph·ch0

{phd·CH0} ifch=ph·CHCH0=term(CH)∪nterm(CH)∧(∗) where (*) is one of the following conditions:

1. nterm(CH)is not empty.

2. phis of the formM(c1∨ · · · ∨cm)+/ω.

3. orphcontains at least one tail-divergent CE.

Example 5.61. The initial chains of CRsubtrareM(4∨5)+{(3)}and(3)(see Examples5.53and5.55).

The terminating chains of subtr are M(4c ∨5c)+{(3c)} and (3c). CR subtr does not have any non-terminating chain because we have discarded the only divergent path (Example 5.55) andsubtr does not have tail-divergent CEs. Consequently, the partial versions of CEs do not appear in any chain and they do not need to be considered.

Theorem5.25(Refined Chains Completeness) remains unchanged but the proof has to be extended to consider chains with multiple recursion.

Proof of Theorem5.25for Multiple Recursion. The proof for finite evaluations (Equation 5.3) can be ap-plied directly for chains with multiple recursion considering the updated version of the functionsp be-cause the reasoning does not rely on the structure of the chains.

For infinite evaluations (Equation 5.4). Let T0 ∈ ¹CRS0|C(a : bω, we know that there is a T

¹CRS|C(a : bω such that sp(T) = T0. For T, there is a chain chsuch that T ∈ ¹CRS|C[ch](a : bω

(because of Theorem5.9). Therefore, it is enough to prove that allp0pathsC(sp(T))match a chain in nterm(ch). The original proof was done by induction on the structure of the chain and it can be reused.

However, there is an additional case in the induction step, whench=ph·CH, that needs to be considered.

• If the phase is iterative, there is only one chainchω =phd·CH0 that can be generated such that CH0:=term(CH)∪nterm(CH). If T is infinite, at least one of the three conditions from nterm’s definition is guaranteed to hold sochωis indeed generated.

Let ppathsC(T), we have to prove that the corresponding path p0pathsC(sp(T))matcheschω. Ifpcan be split into two sub-paths p=p1·p2 such that p1 matchesphand p2 matchesch0CH.

The path p0 can be split at the same point p0= p01·p02 and by induction hypothesis p02 matches a chain inCH0. The phasephd contains all the partial, complete, and tail-divergent CEs ofphso p01 has to matchphd and label(p0)∈ Lchω. If pcannot be split, then pmatches phcompletely and p0 matchesphd. Therefore,label(p0)∈ Lchω.

• If the phase is not iterativeph= (c), there is one refined chain chωi :=cpi·CH0 for each partial CEcpi and onechω:=ctd·CH0 ifcis tail-divergent. We prove that all the paths insp(T) match a singlechωi or matchchω. Every pathppathsC(T)has the formp=c(a:b)·p1 wherec(a:b)is

the node of the root of the evaluation T (common to all paths) andp1 matches a chainch0CH.

Similarly, every p0pathsC(sp(T)) has the form p0 = X(a : bp01 where X is cpi for some i or ctd but the same for all paths. By induction hypothesis p01 matches a chain inCH0. Therefore, all p0pathsC(sp(T))match a single chainX ·CH0which can bechωi orchω.

5.5.3 Invariants