doc: emphasize that we are dealing with integer sets
[platform/upstream/isl.git] / doc / implementation.tex
1 \section{Sets and Relations}
2
3 \begin{definition}[Polyhedral Set]
4 A {\em polyhedral set}\index{polyhedral set} $S$ is a finite union of basic sets
5 $S = \bigcup_i S_i$, each of which can be represented using affine
6 constraints
7 $$
8 S_i : \Z^n \to 2^{\Z^d} : \vec s \mapsto
9 S_i(\vec s) =
10 \{\, \vec x \in \Z^d \mid \exists \vec z \in \Z^e :
11 A \vec x + B \vec s + D \vec z + \vec c \geq \vec 0 \,\}
12 ,
13 $$
14 with $A \in \Z^{m \times d}$,
15 $B \in \Z^{m \times n}$,
16 $D \in \Z^{m \times e}$
17 and $\vec c \in \Z^m$.
18 \end{definition}
19
20 \begin{definition}[Parameter Domain of a Set]
21 Let $S \in \Z^n \to 2^{\Z^d}$ be a set.
22 The {\em parameter domain} of $S$ is the set
23 $$\pdom S \coloneqq \{\, \vec s \in \Z^n \mid S(\vec s) \ne \emptyset \,\}.$$
24 \end{definition}
25
26 \begin{definition}[Polyhedral Relation]
27 A {\em polyhedral relation}\index{polyhedral relation}
28 $R$ is a finite union of basic relations
29 $R = \bigcup_i R_i$ of type
30 $\Z^n \to 2^{\Z^{d_1+d_2}}$,
31 each of which can be represented using affine
32 constraints
33 $$
34 R_i = \vec s \mapsto
35 R_i(\vec s) =
36 \{\, \vec x_1 \to \vec x_2 \in \Z^{d_1} \times \Z^{d_2}
37 \mid \exists \vec z \in \Z^e :
38 A_1 \vec x_1 + A_2 \vec x_2 + B \vec s + D \vec z + \vec c \geq \vec 0 \,\}
39 ,
40 $$
41 with $A_i \in \Z^{m \times d_i}$,
42 $B \in \Z^{m \times n}$,
43 $D \in \Z^{m \times e}$
44 and $\vec c \in \Z^m$.
45 \end{definition}
46
47 \begin{definition}[Parameter Domain of a Relation]
48 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation.
49 The {\em parameter domain} of $R$ is the set
50 $$\pdom R \coloneqq \{\, \vec s \in \Z^n \mid R(\vec s) \ne \emptyset \,\}.$$
51 \end{definition}
52
53 \begin{definition}[Domain of a Relation]
54 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation.
55 The {\em domain} of $R$ is the polyhedral set
56 $$\domain R \coloneqq \vec s \mapsto
57 \{\, \vec x_1 \in \Z^{d_1} \mid \exists \vec x_2 \in \Z^{d_2} :
58 (\vec x_1, \vec x_2) \in R(\vec s) \,\}
59 .
60 $$
61 \end{definition}
62
63 \begin{definition}[Range of a Relation]
64 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation.
65 The {\em range} of $R$ is the polyhedral set
66 $$
67 \range R \coloneqq \vec s \mapsto
68 \{\, \vec x_2 \in \Z^{d_2} \mid \exists \vec x_1 \in \Z^{d_1} :
69 (\vec x_1, \vec x_2) \in R(\vec s) \,\}
70 .
71 $$
72 \end{definition}
73
74 \begin{definition}[Composition of Relations]
75 Let $R \in \Z^n \to 2^{\Z^{d_1+d_2}}$ and
76 $S \in \Z^n \to 2^{\Z^{d_2+d_3}}$ be two relations,
77 then the composition of
78 $R$ and $S$ is defined as
79 $$
80 S \circ R \coloneqq
81 \vec s \mapsto
82 \{\, \vec x_1 \to \vec x_3 \in \Z^{d_1} \times \Z^{d_3}
83 \mid \exists \vec x_2 \in \Z^{d_2} :
84 \vec x_1 \to \vec x_2 \in R(\vec s) \wedge
85 \vec x_2 \to \vec x_3 \in S(\vec s)
86 \,\}
87 .
88 $$
89 \end{definition}
90
91 \begin{definition}[Difference Set of a Relation]
92 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation.
93 The difference set ($\Delta \, R$) of $R$ is the set
94 of differences between image elements and the corresponding
95 domain elements,
96 $$
97 \Delta \, R \coloneqq
98 \vec s \mapsto
99 \{\, \vec \delta \in \Z^{d} \mid \exists \vec x \to \vec y \in R :
100 \vec \delta = \vec y - \vec x
101 \,\}
102 $$
103 \end{definition}
104
105 \section{Coalescing}\label{s:coalescing}
106
107 See \shortciteN{Verdoolaege2009isl}, for now.
108 More details will be added later.
109
110 \section{Transitive Closure}
111
112 \subsection{Introduction}
113
114 \begin{definition}[Power of a Relation]
115 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation and
116 $k \in \Z_{\ge 1}$
117 a positive number, then power $k$ of relation $R$ is defined as
118 \begin{equation}
119 \label{eq:transitive:power}
120 R^k \coloneqq
121 \begin{cases}
122 R & \text{if $k = 1$}
123 \\
124 R \circ R^{k-1} & \text{if $k \ge 2$}
125 .
126 \end{cases}
127 \end{equation}
128 \end{definition}
129
130 \begin{definition}[Transitive Closure of a Relation]
131 Let $R \in \Z^n \to 2^{\Z^{d+d}}$ be a relation,
132 then the transitive closure $R^+$ of $R$ is the union
133 of all positive powers of $R$,
134 $$
135 R^+ \coloneqq \bigcup_{k \ge 1} R^k
136 .
137 $$
138 \end{definition}
139 Alternatively, the transitive closure may be defined
140 inductively as
141 \begin{equation}
142 \label{eq:transitive:inductive}
143 R^+ \coloneqq R \cup \left(R \circ R^+\right)
144 .
145 \end{equation}
146
147 Since the transitive closure of a polyhedral relation
148 may no longer be a polyhedral relation \shortcite{Kelly1996closure},
149 we can, in the general case, only compute an approximation
150 of the transitive closure.
151 Whereas \shortciteN{Kelly1996closure} compute underapproximations,
152 we, like \shortciteN{Beletska2009}, compute overapproximations.
153 That is, given a relation $R$, we will compute a relation $T$
154 such that $R^+ \subseteq T$.  Of course, we want this approximation
155 to be as close as possible to the actual transitive closure
156 $R^+$ and we want to detect the cases where the approximation is
157 exact, i.e., where $T = R^+$.
158
159 For computing an approximation of the transitive closure of $R$,
160 we follow the same general strategy as \shortciteN{Beletska2009}
161 and first compute an approximation of $R^k$ for $k \ge 1$ and then project
162 out the parameter $k$ from the resulting relation.
163
164 \begin{example}
165 As a trivial example, consider the relation
166 $R = \{\, x \to x + 1 \,\}$.  The $k$th power of this map
167 for arbitrary $k$ is
168 $$
169 R^k = k \mapsto \{\, x \to x + k \mid k \ge 1 \,\}
170 .
171 $$
172 The transitive closure is then
173 $$
174 \begin{aligned}
175 R^+ & = \{\, x \to y \mid \exists k \in \Z_{\ge 1} : y = x + k \,\}
176 \\
177 & = \{\, x \to y \mid y \ge x + 1 \,\}
178 .
179 \end{aligned}
180 $$
181 \end{example}
182
183 \subsection{Computing an Approximation of $R^k$}
184 \label{s:power}
185
186 There are some special cases where the computation of $R^k$ is very easy.
187 One such case is that where $R$ does not compose with itself,
188 i.e., $R \circ R = \emptyset$ or $\domain R \cap \range R = \emptyset$.
189 In this case, $R^k$ is only non-empty for $k=1$ where it is equal
190 to $R$ itself.
191
192 In general, it is impossible to construct a closed form
193 of $R^k$ as a polyhedral relation.
194 We will therefore need to make some approximations.
195 As a first approximations, we will consider each of the basic
196 relations in $R$ as simply adding one or more offsets to a domain element
197 to arrive at an image element and ignore the fact that some of these
198 offsets may only be applied to some of the domain elements.
199 That is, we will only consider the difference set $\Delta\,R$ of the relation.
200 In particular, we will first construct a collection $P$ of paths
201 that move through
202 a total of $k$ offsets and then intersect domain and range of this
203 collection with those of $R$.
204 That is, 
205 \begin{equation}
206 \label{eq:transitive:approx}
207 K = P \cap \left(\domain R \to \range R\right)
208 ,
209 \end{equation}
210 with
211 \begin{equation}
212 \label{eq:transitive:path}
213 P = \vec s \mapsto \{\, \vec x \to \vec y \mid
214 \exists k_i \in \Z_{\ge 0} :
215 \vec y = \vec x + \sum_i k_i \, \Delta_i(\vec s)
216 \wedge
217 \sum_i k_i = k > 0
218 \,\}
219 \end{equation}
220 and with $\Delta_i$ the basic sets that compose
221 the difference set $\Delta\,R$.
222 Note that the number of basic sets $\Delta_i$ need not be
223 the same as the number of basic relations in $R$.
224 Also note that since addition is commutative, it does not
225 matter in which order we add the offsets and so we are allowed
226 to group them as we did in \eqref{eq:transitive:path}.
227
228 If all the $\Delta_i$s are singleton sets
229 $\Delta_i = \{\, \vec \delta_i \,\}$ with $\vec \delta_i \in \Z^d$,
230 then \eqref{eq:transitive:path} simplifies to
231 \begin{equation}
232 \label{eq:transitive:singleton}
233 P = \{\, \vec x \to \vec y \mid
234 \exists k_i \in \Z_{\ge 0} :
235 \vec y = \vec x + \sum_i k_i \, \vec \delta_i
236 \wedge
237 \sum_i k_i = k > 0
238 \,\}
239 \end{equation}
240 and then the approximation computed in \eqref{eq:transitive:approx}
241 is essentially the same as that of \shortciteN{Beletska2009}.
242 If some of $\Delta_i$s are not singleton sets or if
243 some of $\vec \delta_i$s are parametric, then we need
244 to resort to further approximations.
245
246 To ease both the exposition and the implementation, we will for
247 the remainder of this section work with extended offsets
248 $\Delta_i' = \Delta_i \times \{\, 1 \,\}$.
249 That is, each offset is extended with an extra coordinate that is
250 set equal to one.  The paths constructed by summing such extended
251 offsets have the length encoded as the difference of their
252 final coordinates.  The path $P'$ can then be decomposed into
253 paths $P_i'$, one for each $\Delta_i$,
254 \begin{equation}
255 \label{eq:transitive:decompose}
256 P' = \left(
257 (P_m' \cup \identity) \circ \cdots \circ
258 (P_2' \cup \identity) \circ
259 (P_1' \cup \identity)
260 \right) \cap
261 \{\,
262 \vec x' \to \vec y' \mid y_{d+1} - x_{d+1} = k > 0
263 \,\}
264 ,
265 \end{equation}
266 with
267 $$
268 P_i' = \vec s \mapsto \{\, \vec x' \to \vec y' \mid
269 \exists k \in \Z_{\ge 1} :
270 \vec y' = \vec x' + k \, \Delta_i'(\vec s)
271 \,\}
272 .
273 $$
274 Note that each $P_i'$ contains paths of length at least one.
275 We therefore need to take the union with the identity relation
276 when composing the $P_i'$s to allow for paths that do not contain
277 any offsets from one or more $\Delta_i'$.
278 The path that consists of only identity relations is removed
279 by imposing the constraint $y_{d+1} - x_{d+1} > 0$.
280 Taking the union with the identity relation means that
281 that the relations we compose in \eqref{eq:transitive:decompose}
282 each consist of two basic relations.  If there are $m$
283 disjuncts in the input relation, then a direct application
284 of the composition operation may therefore result in a relation
285 with $2^m$ disjuncts, which is prohibitively expensive.
286 It is therefore crucial to apply coalescing (\autoref{s:coalescing})
287 after each composition.
288
289 Let us now consider how to compute an overapproximation of $P_i'$.
290 Those that correspond to singleton $\Delta_i$s are grouped together
291 and handled as in \eqref{eq:transitive:singleton}.
292 Note that this is just an optimization.  The procedure described
293 below would produce results that are at least as accurate.
294 For simplicity, we first assume that no constraint in $\Delta_i'$
295 involves any existentially quantified variables.
296 We will return to existentially quantified variables at the end
297 of this section.
298 Without existentially quantified variables, we can classify
299 the constraints of $\Delta_i'$ as follows
300 \begin{enumerate}
301 \item non-parametric constraints
302 \begin{equation}
303 \label{eq:transitive:non-parametric}
304 A_1 \vec x + \vec c_1 \geq \vec 0
305 \end{equation}
306 \item purely parametric constraints
307 \begin{equation}
308 \label{eq:transitive:parametric}
309 B_2 \vec s + \vec c_2 \geq \vec 0
310 \end{equation}
311 \item negative mixed constraints
312 \begin{equation}
313 \label{eq:transitive:mixed}
314 A_3 \vec x + B_3 \vec s + \vec c_3 \geq \vec 0
315 \end{equation}
316 such that for each row $j$ and for all $\vec s$,
317 $$
318 \Delta_i'(\vec s) \cap
319 \{\, \vec x' \to \vec y' \mid B_{3,j} \vec s + c_{3,j} > 0 \,\}
320 = \emptyset
321 $$
322 \item positive mixed constraints
323 $$
324 A_4 \vec x + B_4 \vec s + \vec c_4 \geq \vec 0
325 $$
326 such that for each row $j$, there is at least one $\vec s$ such that
327 $$
328 \Delta_i'(\vec s) \cap
329 \{\, \vec x' \to \vec y' \mid B_{4,j} \vec s + c_{4,j} > 0 \,\}
330 \ne \emptyset
331 $$
332 \end{enumerate}
333 We will use the following approximation $Q_i$ for $P_i'$:
334 \begin{equation}
335 \label{eq:transitive:Q}
336 \begin{aligned}
337 Q_i = \vec s \mapsto
338 \{\,
339 \vec x' \to \vec y'
340 \mid {} & \exists k \in \Z_{\ge 1}, \vec f \in \Z^d :
341 \vec y' = \vec x' + (\vec f, k)
342 \wedge {}
343 \\
344 &
345 A_1 \vec f + k \vec c_1 \geq \vec 0
346 \wedge
347 B_2 \vec s + \vec c_2 \geq \vec 0
348 \wedge
349 A_3 \vec f + B_3 \vec s + \vec c_3 \geq \vec 0
350 \,\}
351 .
352 \end{aligned}
353 \end{equation}
354 To prove that $Q_i$ is indeed an overapproximation of $P_i'$,
355 we need to show that for every $\vec s \in \Z^n$, for every
356 $k \in \Z_{\ge 1}$ and for every $\vec f \in k \, \Delta_i(\vec s)$
357 we have that
358 $(\vec f, k)$ satisfies the constraints in \eqref{eq:transitive:Q}.
359 If $\Delta_i(\vec s)$ is non-empty, then $\vec s$ must satisfy
360 the constraints in \eqref{eq:transitive:parametric}.
361 Each element $(\vec f, k) \in k \, \Delta_i'(\vec s)$ is a sum
362 of $k$ elements $(\vec f_j, 1)$ in $\Delta_i'(\vec s)$.
363 Each of these elements satisfies the constraints in
364 \eqref{eq:transitive:non-parametric}, i.e.,
365 $$
366 \left[
367 \begin{matrix}
368 A_1 & \vec c_1
369 \end{matrix}
370 \right]
371 \left[
372 \begin{matrix}
373 \vec f_j \\ 1
374 \end{matrix}
375 \right]
376 \ge \vec 0
377 .
378 $$
379 The sum of these elements therefore satisfies the same set of inequalities,
380 i.e., $A_1 \vec f + k \vec c_1 \geq \vec 0$.
381 Finally, the constraints in \eqref{eq:transitive:mixed} are such
382 that for any $\vec s$ in the parameter domain of $\Delta$,
383 we have $-\vec r(\vec s) \coloneqq B_3 \vec s + \vec c_3 \le \vec 0$,
384 i.e., $A_3 \vec f_j \ge \vec r(\vec s) \ge \vec 0$
385 and therefore also $A_3 \vec f \ge \vec r(\vec s)$.
386 Note that if there are no mixed constraints and if the
387 rational relaxation of $\Delta_i(\vec s)$, i.e.,
388 $\{\, \vec x \in \Q^d \mid A_1 \vec x + \vec c_1 \ge \vec 0\,\}$,
389 has integer vertices, then the approximation is exact, i.e.,
390 $Q_i = P_i'$.  In this case, the vertices of $\Delta'_i(\vec s)$
391 generate the rational cone
392 $\{\, \vec x' \in \Q^{d+1} \mid \left[
393 \begin{matrix}
394 A_1 & \vec c_1
395 \end{matrix}
396 \right] \vec x' \,\}$ and therefore $\Delta'_i(\vec s)$ is
397 a Hilbert basis of this cone \shortcite[Theorem~16.4]{Schrijver1986}.
398
399 Existentially quantified variables can be handled by
400 classifying them into variables that are uniquely
401 determined by the parameters, variables that are independent
402 of the parameters and others.  The first set can be treated
403 as parameters and the second as variables.  Constraints involving
404 the other existentially quantified variables are removed.
405
406 \begin{example}
407 Consider the relation
408 $$
409 R =
410 n \to \{\, x \to y \mid \exists \, \alpha_0, \alpha_1: 7\alpha_0 = -2 + n \wedge 5\alpha_1 = -1 - x + y \wedge y \ge 6 + x \,\}
411 .
412 $$
413 The difference set of this relation is
414 $$
415 \Delta = \Delta \, R =
416 n \to \{\, x \mid \exists \, \alpha_0, \alpha_1: 7\alpha_0 = -2 + n \wedge 5\alpha_1 = -1 + x \wedge x \ge 6 \,\}
417 .
418 $$
419 The existentially quantified variables can be defined in terms
420 of the parameters and variables as
421 $$
422 \alpha_0 = \floor{\frac{-2 + n}7}
423 \qquad
424 \text{and}
425 \qquad
426 \alpha_1 = \floor{\frac{-1 + x}5}
427 .
428 $$
429 $\alpha_0$ can therefore be treated as a parameter,
430 while $\alpha_1$ can be treated as a variable.
431 This in turn means that $7\alpha_0 = -2 + n$ can be treated as
432 a purely parametric constraint, while the other two constraints are
433 non-parametric.
434 The corresponding $Q$~\eqref{eq:transitive:Q} is therefore
435 $$
436 \begin{aligned}
437 n \to \{\, (x,z) \to (y,w) \mid
438 \exists\, \alpha_0, \alpha_1, k, f : {} &
439 k \ge 1 \wedge
440 y = x + f \wedge
441 w = z + k \wedge {} \\
442 &
443 7\alpha_0 = -2 + n \wedge
444 5\alpha_1 = -k + x \wedge
445 x \ge 6 k
446 \,\}
447 .
448 \end{aligned}
449 $$
450 Projecting out the final coordinates encoding the length of the paths,
451 results in the exact transitive closure
452 $$
453 R^+ =
454 n \to \{\, x \to y \mid \exists \, \alpha_0, \alpha_1: 7\alpha_1 = -2 + n \wedge 6\alpha_0 \ge -x + y \wedge 5\alpha_0 \le -1 - x + y \,\}
455 .
456 $$
457 \end{example}
458
459 \subsection{Checking Exactness}
460
461 The approximation $T$ for the transitive closure $R^+$ can be obtained
462 by projecting out the parameter $k$ from the approximation $K$
463 \eqref{eq:transitive:approx} of the power $R^k$.
464 Since $K$ is an overapproximation of $R^k$, $T$ will also be an
465 overapproximation of $R^+$.
466 To check whether the results are exact, we need to consider two
467 cases depending on whether $R$ is {\em cyclic}, where $R$ is defined
468 to be cyclic if $R^+$ maps any element to itself, i.e.,
469 $R^+ \cap \identity \ne \emptyset$.
470 If $R$ is acyclic, then the inductive definition of
471 \eqref{eq:transitive:inductive} is equivalent to its completion,
472 i.e.,
473 $$
474 R^+ = R \cup \left(R \circ R^+\right)
475 $$
476 is a defining property.
477 Since $T$ is known to be an overapproximation, we only need to check
478 whether
479 $$
480 T \subseteq R \cup \left(R \circ T\right)
481 .
482 $$
483 This is essentially Theorem~5 of \shortciteN{Kelly1996closure}.
484 The only difference is that they only consider lexicographically
485 forward relations, a special case of acyclic relations.
486
487 If, on the other hand, $R$ is cyclic, then we have to resort
488 to checking whether the approximation $K$ of the power is exact.
489 Note that $T$ may be exact even if $K$ is not exact, so the check
490 is sound, but incomplete.
491 To check exactness of the power, we simply need to check
492 \eqref{eq:transitive:power}.  Since again $K$ is known
493 to be an overapproximation, we only need to check whether
494 $$
495 \begin{aligned}
496 K'|_{y_{d+1} - x_{d+1} = 1} & \subseteq R'
497 \\
498 K'|_{y_{d+1} - x_{d+1} \ge 2} & \subseteq R' \circ K'|_{y_{d+1} - x_{d+1} \ge 1}
499 ,
500 \end{aligned}
501 $$
502 where $R' = \{\, \vec x' \to \vec y' \mid \vec x \to \vec y \in R
503 \wedge y_{d+1} - x_{d+1} = 1\,\}$, i.e., $R$ extended with path
504 lengths equal to 1.
505
506 All that remains is to explain how to check the cyclicity of $R$.
507 Note that the exactness on the power is always sound, even
508 in the acyclic case, so we only need to be careful that we find
509 all cyclic cases.  Now, if $R$ is cyclic, i.e.,
510 $R^+ \cap \identity \ne \emptyset$, then, since $T$ is
511 an overapproximation of $R^+$, also
512 $T \cap \identity \ne \emptyset$.  This in turn means
513 that $\Delta \, K'$ contains a point whose first $d$ coordinates
514 are zero and whose final coordinate is positive.
515 In the implementation we currently perform this test on $P'$ instead of $K'$.
516 Note that if $R^+$ is acyclic and $T$ is not, then the approximation
517 is clearly not exact and the approximation of the power $K$
518 will not be exact either.
519
520 \subsection{Decomposing $R$ into strongly connected components}
521
522 If the input relation $R$ is a union of several basic relations
523 that can be partially ordered
524 then the accuracy of the approximation may be improved by computing
525 an approximation of each strongly connected components separately.
526 For example, if $R = R_1 \cup R_2$ and $R_1 \circ R_2 = \emptyset$,
527 then we know that any path that passes through $R_2$ cannot later
528 pass through $R_1$, i.e.,
529 $$
530 R^+ = R_1^+ \cup R_2^+ \cup \left(R_2^+ \circ R_1^+\right)
531 .
532 $$
533 We can therefore compute (approximations of) transitive closures
534 of $R_1$ and $R_2$ separately.
535 Note, however, that the condition $R_1 \circ R_2 = \emptyset$
536 is actually too strong.
537 If $R_1 \circ R_2$ is a subset of $R_2 \circ R_1$
538 then we can reorder the segments
539 in any path that moves through both $R_1$ and $R_2$ to
540 first move through $R_1$ and then through $R_2$.
541
542 This idea can be generalized to relations that are unions
543 of more than two basic relations by constructing the
544 strongly connected components in the graph with as vertices
545 the basic relations and an edge between two basic relations
546 $R_i$ and $R_j$ if $R_i$ needs to follow $R_j$ in some paths.
547 That is, there is an edge from $R_i$ to $R_j$ iff
548 \begin{equation}
549 \label{eq:transitive:edge}
550 R_i \circ R_j
551 \not\subseteq
552 R_j \circ R_i
553 .
554 \end{equation}
555 The components can be obtained from the graph by applying
556 Tarjan's algorithm \shortcite{Tarjan1972}.
557
558 In practice, we compute the (extended) powers $K_i'$ of each component
559 separately and then compose them as in \eqref{eq:transitive:decompose}.
560 Note, however, that in this case the order in which we apply them is
561 important and should correspond to a topological ordering of the
562 strongly connected components.  Simply applying Tarjan's
563 algorithm will produce topologically sorted strongly connected components.
564 The graph on which Tarjan's algorithm is applied is constructed on-the-fly.
565 That is, whenever the algorithm checks if there is an edge between
566 two vertices, we evaluate \eqref{eq:transitive:edge}.
567 The exactness check is performed on each component separately.
568 If the approximation turns out to be inexact for any of the components,
569 then the entire result is marked inexact and the exactness check
570 is skipped on the components that still need to be handled.
571
572 \begin{figure}
573 \begin{center}
574 \begin{tikzpicture}[x=0.5cm,y=0.5cm,>=stealth,shorten >=1pt]
575 \foreach \x in {1,...,10}{
576     \foreach \y in {1,...,10}{
577         \draw[->] (\x,\y) -- (\x,\y+1);
578     }
579 }
580 \foreach \x in {1,...,20}{
581     \foreach \y in {5,...,15}{
582         \draw[->] (\x,\y) -- (\x+1,\y);
583     }
584 }
585 \end{tikzpicture}
586 \end{center}
587 \caption{The relation from \autoref{ex:closure4}}
588 \label{f:closure4}
589 \end{figure}
590 \begin{example}
591 \label{ex:closure4}
592 Consider the relation in example {\tt closure4} that comes with
593 the Omega calculator~\shortcite{Omega_calc}, $R = R_1 \cup R_2$,
594 with
595 $$
596 \begin{aligned}
597 R_1 & = \{\, (x,y) \to (x,y+1) \mid 1 \le x,y \le 10 \,\}
598 \\
599 R_2 & = \{\, (x,y) \to (x+1,y) \mid 1 \le x \le 20 \wedge 5 \le y \le 15 \,\}
600 .
601 \end{aligned}
602 $$
603 This relation is shown graphically in \autoref{f:closure4}.
604 We have
605 $$
606 \begin{aligned}
607 R_1 \circ R_2 &=
608 \{\, (x,y) \to (x+1,y+1) \mid 1 \le x \le 9 \wedge 5 \le y \le 10 \,\}
609 \\
610 R_2 \circ R_1 &=
611 \{\, (x,y) \to (x+1,y+1) \mid 1 \le x \le 10 \wedge 4 \le y \le 10 \,\}
612 .
613 \end{aligned}
614 $$
615 Clearly, $R_1 \circ R_2 \subseteq R_2 \circ R_1$ and so
616 $$
617 \left(
618 R_1 \cup R_2
619 \right)^+
620 =
621 \left(R_2^+ \circ R_1^+\right)
622 \cup R_1^+
623 \cup R_2^+
624 .
625 $$
626 \end{example}
627
628 \begin{figure}
629 \newcounter{n}
630 \newcounter{t1}
631 \newcounter{t2}
632 \newcounter{t3}
633 \newcounter{t4}
634 \begin{center}
635 \begin{tikzpicture}[>=stealth,shorten >=1pt]
636 \setcounter{n}{7}
637 \foreach \i in {1,...,\value{n}}{
638     \foreach \j in {1,...,\value{n}}{
639         \setcounter{t1}{2 * \j - 4 - \i + 1}
640         \setcounter{t2}{\value{n} - 3 - \i + 1}
641         \setcounter{t3}{2 * \i - 1 - \j + 1}
642         \setcounter{t4}{\value{n} - \j + 1}
643         \ifnum\value{t1}>0\ifnum\value{t2}>0
644         \ifnum\value{t3}>0\ifnum\value{t4}>0
645             \draw[thick,->] (\i,\j) to[out=20] (\i+3,\j);
646         \fi\fi\fi\fi
647         \setcounter{t1}{2 * \j - 1 - \i + 1}
648         \setcounter{t2}{\value{n} - \i + 1}
649         \setcounter{t3}{2 * \i - 4 - \j + 1}
650         \setcounter{t4}{\value{n} - 3 - \j + 1}
651         \ifnum\value{t1}>0\ifnum\value{t2}>0
652         \ifnum\value{t3}>0\ifnum\value{t4}>0
653             \draw[thick,->] (\i,\j) to[in=-20,out=20] (\i,\j+3);
654         \fi\fi\fi\fi
655         \setcounter{t1}{2 * \j - 1 - \i + 1}
656         \setcounter{t2}{\value{n} - 1 - \i + 1}
657         \setcounter{t3}{2 * \i - 1 - \j + 1}
658         \setcounter{t4}{\value{n} - 1 - \j + 1}
659         \ifnum\value{t1}>0\ifnum\value{t2}>0
660         \ifnum\value{t3}>0\ifnum\value{t4}>0
661             \draw[thick,->] (\i,\j) to (\i+1,\j+1);
662         \fi\fi\fi\fi
663     }
664 }
665 \end{tikzpicture}
666 \end{center}
667 \caption{The relation from \autoref{ex:decomposition}}
668 \label{f:decomposition}
669 \end{figure}
670 \begin{example}
671 \label{ex:decomposition}
672 Consider the relation on the right of \shortciteN[Figure~2]{Beletska2009},
673 reproduced in \autoref{f:decomposition}.
674 The relation can be described as $R = R_1 \cup R_2 \cup R_3$,
675 with
676 $$
677 \begin{aligned}
678 R_1 &= n \mapsto \{\, (i,j) \to (i+3,j) \mid
679 i \le 2 j - 4 \wedge
680 i \le n - 3 \wedge
681 j \le 2 i - 1 \wedge
682 j \le n \,\}
683 \\
684 R_2 &= n \mapsto \{\, (i,j) \to (i,j+3) \mid
685 i \le 2 j - 1 \wedge
686 i \le n \wedge
687 j \le 2 i - 4 \wedge
688 j \le n - 3 \,\}
689 \\
690 R_3 &= n \mapsto \{\, (i,j) \to (i+1,j+1) \mid
691 i \le 2 j - 1 \wedge
692 i \le n - 1 \wedge
693 j \le 2 i - 1 \wedge
694 j \le n - 1\,\}
695 .
696 \end{aligned}
697 $$
698 The figure shows this relation for $n = 7$.
699 Both
700 $R_3 \circ R_1 \subseteq R_1 \circ R_3$
701 and
702 $R_3 \circ R_2 \subseteq R_2 \circ R_3$,
703 which the reader can verify using the {\tt iscc} calculator:
704 \begin{verbatim}
705 R1 := [n] -> { [i,j] -> [i+3,j] : i <= 2 j - 4 and i <= n - 3 and
706                                   j <= 2 i - 1 and j <= n };
707 R2 := [n] -> { [i,j] -> [i,j+3] : i <= 2 j - 1 and i <= n and
708                                   j <= 2 i - 4 and j <= n - 3 };
709 R3 := [n] -> { [i,j] -> [i+1,j+1] : i <= 2 j - 1 and i <= n - 1 and
710                                     j <= 2 i - 1 and j <= n - 1 };
711 (R1 . R3) - (R3 . R1);
712 (R2 . R3) - (R3 . R2);
713 \end{verbatim}
714 $R_3$ can therefore be moved forward in any path.
715 For the other two basic relations, we have both
716 $R_2 \circ R_1 \not\subseteq R_1 \circ R_2$
717 and
718 $R_1 \circ R_2 \not\subseteq R_2 \circ R_1$
719 and so $R_1$ and $R_2$ form a strongly connected component.
720 By computing the power of $R_3$ and $R_1 \cup R_2$ separately
721 and composing the results, the power of $R$ can be computed exactly
722 using \eqref{eq:transitive:singleton}.
723 As explained by \shortciteN{Beletska2009}, applying the same formula
724 to $R$ directly, without a decomposition, would result in
725 an overapproximation of the power.
726 \end{example}
727
728 \subsection{Partitioning the domains and ranges of $R$}
729
730 The algorithm of \autoref{s:power} assumes that the input relation $R$
731 can be treated as a union of translations.
732 This is a reasonable assumption if $R$ maps elements of a given
733 abstract domain to the same domain.
734 However, if $R$ is a union of relations that map between different
735 domains, then this assumption no longer holds.
736 In particular, when an entire dependence graph is encoded
737 in a single relation, as is done by, e.g.,
738 \shortciteN[Section~6.1]{Barthou2000MSE}, then it does not make
739 sense to look at differences between iterations of different domains.
740 Now, arguably, a modified Floyd-Warshall algorithm should
741 be applied to the dependence graph, as advocated by
742 \shortciteN{Kelly1996closure}, with the transitive closure operation
743 only being applied to relations from a given domain to itself.
744 However, it is also possible to detect disjoint domains and ranges
745 and to apply Floyd-Warshall internally.
746
747 \linesnumbered
748 \begin{algorithm}
749 \caption{The modified Floyd-Warshall algorithm of
750 \protect\shortciteN{Kelly1996closure}}
751 \label{a:Floyd}
752 \SetKwInput{Input}{Input}
753 \SetKwInput{Output}{Output}
754 \Input{Relations $R_{pq}$, $0 \le p, q < n$}
755 \Output{Updated relations $R_{pq}$ such that each relation
756 $R_{pq}$ contains all indirect paths from $p$ to $q$ in the input graph}
757 %
758 \BlankLine
759 \SetVline
760 \dontprintsemicolon
761 %
762 \For{$r \in [0, n-1]$}{
763     $R_{rr} \coloneqq R_{rr}^+$ \nllabel{l:Floyd:closure}\;
764     \For{$p \in [0, n-1]$}{
765         \For{$q \in [0, n-1]$}{
766             \If{$p \ne r$ or $q \ne r$}{
767                 $R_{pq} \coloneqq R_{pq} \cup \left(R_{rq} \circ R_{pr}\right)
768                              \cup \left(R_{rq} \circ R_{rr} \circ R_{pr}\right)$
769              \nllabel{l:Floyd:update}
770              }
771         }
772     }
773 }
774 \end{algorithm}
775
776 Let the input relation $R$ be a union of $m$ basic relations $R_i$.
777 Let $D_{2i}$ be the domains of $R_i$ and $D_{2i+1}$ the ranges of $R_i$.
778 The first step is to group overlapping $D_j$ until a partition is
779 obtained.  If the resulting partition consists of a single part,
780 then we continue with the algorithm of \autoref{s:power}.
781 Otherwise, we apply Floyd-Warshall on the graph with as vertices
782 the parts of the partition and as edges the $R_i$ attached to
783 the appropriate pairs of vertices.
784 In particular, let there be $n$ parts $P_k$ in the partition.
785 We construct $n^2$ relations
786 $$
787 R_{pq} \coloneqq \bigcup_{i \text{ s.t. } \domain R_i \subseteq P_p \wedge
788                                  \range R_i \subseteq P_q} R_i
789 ,
790 $$
791 apply \autoref{a:Floyd} and return the union of all resulting
792 $R_{pq}$ as the transitive closure of $R$.
793 Each iteration of the $r$-loop in \autoref{a:Floyd} updates
794 all relations $R_{pq}$ to include paths that go from $p$ to $r$,
795 possibly stay there for a while, and then go from $r$ to $q$.
796 Note that paths that ``stay in $r$'' include all paths that
797 pass through earlier vertices since $R_{rr}$ itself has been updated
798 accordingly in previous iterations of the outer loop.
799 In principle, it would be sufficient to use the $R_{pr}$
800 and $R_{rq}$ computed in the previous iteration of the
801 $r$-loop in Line~\ref{l:Floyd:update}.
802 However, from an implementation perspective, it is easier
803 to allow either or both of these to have been updated
804 in the same iteration of the $r$-loop.
805 This may result in duplicate paths, but these can usually
806 be removed by coalescing (\autoref{s:coalescing}) the result of the union
807 in Line~\ref{l:Floyd:update}, which should be done in any case.
808 The transitive closure in Line~\ref{l:Floyd:closure}
809 is performed using a recursive call.  This recursive call
810 includes the partitioning step, but the resulting partition will
811 usually be a singleton.
812 The result of the recursive call will either be exact or an
813 overapproximation.  The final result of Floyd-Warshall is therefore
814 also exact or an overapproximation.
815
816 \begin{figure}
817 \begin{center}
818 \begin{tikzpicture}[x=1cm,y=1cm,>=stealth,shorten >=3pt]
819 \foreach \x/\y in {0/0,1/1,3/2} {
820     \fill (\x,\y) circle (2pt);
821 }
822 \foreach \x/\y in {0/1,2/2,3/3} {
823     \draw (\x,\y) circle (2pt);
824 }
825 \draw[->] (0,0) -- (0,1);
826 \draw[->] (0,1) -- (1,1);
827 \draw[->] (2,2) -- (3,2);
828 \draw[->] (3,2) -- (3,3);
829 \draw[->,dashed] (2,2) -- (3,3);
830 \draw[->,dotted] (0,0) -- (1,1);
831 \end{tikzpicture}
832 \end{center}
833 \caption{The relation (solid arrows) on the right of Figure~1 of
834 \protect\shortciteN{Beletska2009} and its transitive closure}
835 \label{f:COCOA:1}
836 \end{figure}
837 \begin{example}
838 Consider the relation on the right of Figure~1 of
839 \shortciteN{Beletska2009},
840 reproduced in \autoref{f:COCOA:1}.
841 This relation can be described as
842 $$
843 \begin{aligned}
844 \{\, (x, y) \to (x_2, y_2) \mid {} & (3y = 2x \wedge x_2 = x \wedge 3y_2 = 3 + 2x \wedge x \ge 0 \wedge x \le 3) \vee {} \\
845 & (x_2 = 1 + x \wedge y_2 = y \wedge x \ge 0 \wedge 3y \ge 2 + 2x \wedge x \le 2 \wedge 3y \le 3 + 2x) \,\}
846 .
847 \end{aligned}
848 $$
849 Note that the domain of the upward relation overlaps with the range
850 of the rightward relation and vice versa, but that the domain
851 of neither relation overlaps with its own range or the domain of
852 the other relation.
853 The domains and ranges can therefore be partitioned into two parts,
854 $P_0$ and $P_1$, shown as the white and black dots in \autoref{f:COCOA:1},
855 respectively.
856 Initially, we have
857 $$
858 \begin{aligned}
859 R_{00} & = \emptyset
860 \\
861 R_{01} & = 
862 \{\, (x, y) \to (x+1, y) \mid 
863 (x \ge 0 \wedge 3y \ge 2 + 2x \wedge x \le 2 \wedge 3y \le 3 + 2x) \,\}
864 \\
865 R_{10} & =
866 \{\, (x, y) \to (x_2, y_2) \mid (3y = 2x \wedge x_2 = x \wedge 3y_2 = 3 + 2x \wedge x \ge 0 \wedge x \le 3) \,\}
867 \\
868 R_{11} & = \emptyset
869 .
870 \end{aligned}
871 $$
872 In the first iteration, $R_{00}$ remains the same ($\emptyset^+ = \emptyset$).
873 $R_{01}$ and $R_{10}$ are therefore also unaffected, but
874 $R_{11}$ is updated to include $R_{01} \circ R_{10}$, i.e.,
875 the dashed arrow in the figure.
876 This new $R_{11}$ is obviously transitively closed, so it is not
877 changed in the second iteration and it does not have an effect
878 on $R_{01}$ and $R_{10}$.  However, $R_{00}$ is updated to
879 include $R_{10} \circ R_{01}$, i.e., the dotted arrow in the figure.
880 The transitive closure of the original relation is then equal to
881 $R_{00} \cup R_{01} \cup R_{10} \cup R_{11}$.
882 \end{example}
883
884 \subsection{An {\tt Omega}-like implementation}
885
886 While the main algorithm of \shortciteN{Kelly1996closure} is
887 designed to compute and underapproximation of the transitive closure,
888 the authors mention that they could also compute overapproximations.
889 In this section, we describe our implementation of an algorithm
890 that is based on their ideas.
891 Note that the {\tt Omega} library computes underapproximations
892 \shortcite[Section 6.4]{Omega_lib}.
893
894 The main tool is Equation~(2) of \shortciteN{Kelly1996closure}.
895 The input relation $R$ is first overapproximated by a ``d-form'' relation
896 $$
897 \{\, \vec i \to \vec j \mid \exists \vec \alpha :
898 \vec L \le \vec j - \vec i \le \vec U
899 \wedge
900 (\forall p : j_p - i_p = M_p \alpha_p)
901 \,\}
902 ,
903 $$
904 where $p$ ranges over the dimensions and $\vec L$, $\vec U$ and
905 $\vec M$ are constant integer vectors.  The elements of $\vec U$
906 may be $\infty$, meaning that there is no upper bound corresponding
907 to that element, and similarly for $\vec L$.
908 Such an overapproximation can be obtained by computing strides,
909 lower and upper bounds on the difference set $\Delta \, R$.
910 The transitive closure of such a ``d-form'' relation is
911 \begin{equation}
912 \label{eq:omega}
913 \{\, \vec i \to \vec j \mid \exists \vec \alpha, k :
914 k \ge 1 \wedge
915 k \, \vec L \le \vec j - \vec i \le k \, \vec U
916 \wedge
917 (\forall p : j_p - i_p = M_p \alpha_p)
918 \,\}
919 .
920 \end{equation}
921 The domain and range of this transitive closure are then
922 intersected with those of the input relation.
923 This is a special case of the algorithm in \autoref{s:power}.
924
925 In their algorithm for computing lower bounds, the authors
926 use the above algorithm as a substep on the disjuncts in the relation.
927 At the end, they say
928 \begin{quote}
929 If an upper bound is required, it can be calculated in a manner
930 similar to that of a single conjunct [sic] relation.
931 \end{quote}
932 Presumably, the authors mean that a ``d-form'' approximation
933 of the whole input relation should be used.
934 However, the accuracy can be improved by also using the following
935 idea from the same paper.  If $R$ is a union of $m$ basic maps,
936 $$
937 R = \bigcup_i R_i
938 ,
939 $$
940 and if we can find an $R_i$ such that for all other $R_j$ we have
941 that
942 $$
943 R_i^* \circ R_j \circ R_i^*
944 $$
945 can be represented as a single basic map, i.e., without a union,
946 then we can compute $R^+$ as
947 $$
948 R^+ = R_i^+ \cup
949 \left(
950 \bigcup_{j \ne i}
951 R_i^* \circ R_j \circ R_i^*
952 \right)^+
953 ,
954 $$
955 reducing the number of disjuncts in the argument of the transitive
956 closure by one.
957 An overapproximation of $R_i^*$ can be obtained by
958 allowing the value zero for $k$ in \eqref{eq:omega},
959 i.e., by computing
960 $$
961 \{\, \vec i \to \vec j \mid \exists \vec \alpha, k :
962 k \ge 0 \wedge
963 k \, \vec L \le \vec j - \vec i \le k \, \vec U
964 \wedge
965 (\forall p : j_p - i_p = M_p \alpha_p)
966 \,\}
967 .
968 $$
969 However, when we intersect domain and range of this relation
970 with those of the input relation, then the result only contains
971 the identity mapping on the intersection of domain and range.
972 \shortciteN{Kelly1996closure} propose to intersect domain
973 and range with then {\em union} of domain and range of the input
974 relation instead and call the result $R_i^?$.
975 Now, this union of domain and range of $R_i$ may not contain
976 the domains and ranges of the whole of $R$.
977 We can therefore not always replace
978 $R_i^* \circ R_j \circ R_i^*$ by
979 $R_i^? \circ R_j \circ R_i^?$.
980 \shortciteN{Kelly1996closure} propose to check the following
981 conditions to decide whether this replacement is justified:
982 $R_i^? - R_i^+$ is not a union and for each $j \ne i$
983 the condition
984 $$
985 \left(R_i^? - R_i^+\right)
986 \circ
987 R_j
988 \circ
989 \left(R_i^? - R_i^+\right)
990 =
991 R_j
992 $$
993 holds.