2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 #include "isl_map_private.h"
16 /* Given a map that represents a path with the length of the path
17 * encoded as the difference between the last output coordindate
18 * and the last input coordinate, set this length to either
19 * exactly "length" (if "exactly" is set) or at least "length"
20 * (if "exactly" is not set).
22 static __isl_give isl_map
*set_path_length(__isl_take isl_map
*map
,
23 int exactly
, int length
)
26 struct isl_basic_map
*bmap
;
35 dim
= isl_map_get_dim(map
);
36 d
= isl_dim_size(dim
, isl_dim_in
);
37 nparam
= isl_dim_size(dim
, isl_dim_param
);
38 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 1);
40 k
= isl_basic_map_alloc_equality(bmap
);
43 k
= isl_basic_map_alloc_inequality(bmap
);
48 isl_seq_clr(c
, 1 + isl_basic_map_total_dim(bmap
));
49 isl_int_set_si(c
[0], -length
);
50 isl_int_set_si(c
[1 + nparam
+ d
- 1], -1);
51 isl_int_set_si(c
[1 + nparam
+ d
+ d
- 1], 1);
53 bmap
= isl_basic_map_finalize(bmap
);
54 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
58 isl_basic_map_free(bmap
);
63 /* Check whether the overapproximation of the power of "map" is exactly
64 * the power of "map". Let R be "map" and A_k the overapproximation.
65 * The approximation is exact if
68 * A_k = A_{k-1} \circ R k >= 2
70 * Since A_k is known to be an overapproximation, we only need to check
73 * A_k \subset A_{k-1} \circ R k >= 2
75 * In practice, "app" has an extra input and output coordinate
76 * to encode the length of the path. So, we first need to add
77 * this coordinate to "map" and set the length of the path to
80 static int check_power_exactness(__isl_take isl_map
*map
,
81 __isl_take isl_map
*app
)
87 map
= isl_map_add(map
, isl_dim_in
, 1);
88 map
= isl_map_add(map
, isl_dim_out
, 1);
89 map
= set_path_length(map
, 1, 1);
91 app_1
= set_path_length(isl_map_copy(app
), 1, 1);
93 exact
= isl_map_is_subset(app_1
, map
);
96 if (!exact
|| exact
< 0) {
102 app_1
= set_path_length(isl_map_copy(app
), 0, 1);
103 app_2
= set_path_length(app
, 0, 2);
104 app_1
= isl_map_apply_range(map
, app_1
);
106 exact
= isl_map_is_subset(app_2
, app_1
);
114 /* Check whether the overapproximation of the power of "map" is exactly
115 * the power of "map", possibly after projecting out the power (if "project"
118 * If "project" is set and if "steps" can only result in acyclic paths,
121 * A = R \cup (A \circ R)
123 * where A is the overapproximation with the power projected out, i.e.,
124 * an overapproximation of the transitive closure.
125 * More specifically, since A is known to be an overapproximation, we check
127 * A \subset R \cup (A \circ R)
129 * Otherwise, we check if the power is exact.
131 * Note that "app" has an extra input and output coordinate to encode
132 * the length of the part. If we are only interested in the transitive
133 * closure, then we can simply project out these coordinates first.
135 static int check_exactness(__isl_take isl_map
*map
, __isl_take isl_map
*app
,
143 return check_power_exactness(map
, app
);
145 d
= isl_map_dim(map
, isl_dim_in
);
146 app
= set_path_length(app
, 0, 1);
147 app
= isl_map_project_out(app
, isl_dim_in
, d
, 1);
148 app
= isl_map_project_out(app
, isl_dim_out
, d
, 1);
150 test
= isl_map_apply_range(isl_map_copy(map
), isl_map_copy(app
));
151 test
= isl_map_union(test
, isl_map_copy(map
));
153 exact
= isl_map_is_subset(app
, test
);
168 * The transitive closure implementation is based on the paper
169 * "Computing the Transitive Closure of a Union of Affine Integer
170 * Tuple Relations" by Anna Beletska, Denis Barthou, Wlodzimierz Bielecki and
174 /* Given a set of n offsets v_i (the rows of "steps"), construct a relation
175 * of the given dimension specification (Z^{n+1} -> Z^{n+1})
176 * that maps an element x to any element that can be reached
177 * by taking a non-negative number of steps along any of
178 * the extended offsets v'_i = [v_i 1].
181 * { [x] -> [y] : exists k_i >= 0, y = x + \sum_i k_i v'_i }
183 * For any element in this relation, the number of steps taken
184 * is equal to the difference in the final coordinates.
186 static __isl_give isl_map
*path_along_steps(__isl_take isl_dim
*dim
,
187 __isl_keep isl_mat
*steps
)
190 struct isl_basic_map
*path
= NULL
;
198 d
= isl_dim_size(dim
, isl_dim_in
);
200 nparam
= isl_dim_size(dim
, isl_dim_param
);
202 path
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), n
, d
, n
);
204 for (i
= 0; i
< n
; ++i
) {
205 k
= isl_basic_map_alloc_div(path
);
208 isl_assert(steps
->ctx
, i
== k
, goto error
);
209 isl_int_set_si(path
->div
[k
][0], 0);
212 for (i
= 0; i
< d
; ++i
) {
213 k
= isl_basic_map_alloc_equality(path
);
216 isl_seq_clr(path
->eq
[k
], 1 + isl_basic_map_total_dim(path
));
217 isl_int_set_si(path
->eq
[k
][1 + nparam
+ i
], 1);
218 isl_int_set_si(path
->eq
[k
][1 + nparam
+ d
+ i
], -1);
220 for (j
= 0; j
< n
; ++j
)
221 isl_int_set_si(path
->eq
[k
][1 + nparam
+ 2 * d
+ j
], 1);
223 for (j
= 0; j
< n
; ++j
)
224 isl_int_set(path
->eq
[k
][1 + nparam
+ 2 * d
+ j
],
228 for (i
= 0; i
< n
; ++i
) {
229 k
= isl_basic_map_alloc_inequality(path
);
232 isl_seq_clr(path
->ineq
[k
], 1 + isl_basic_map_total_dim(path
));
233 isl_int_set_si(path
->ineq
[k
][1 + nparam
+ 2 * d
+ i
], 1);
238 path
= isl_basic_map_simplify(path
);
239 path
= isl_basic_map_finalize(path
);
240 return isl_map_from_basic_map(path
);
243 isl_basic_map_free(path
);
252 /* Return PURE_PARAM if only the coefficients of the parameters are non-zero.
253 * Return PURE_VAR if only the coefficients of the set variables are non-zero.
254 * Return MIXED if only the coefficients of the parameters and the set
255 * variables are non-zero and if moreover the parametric constant
256 * can never attain positive values.
257 * Return IMPURE otherwise.
259 static int purity(__isl_keep isl_basic_set
*bset
, isl_int
*c
, int *div_purity
,
270 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
271 d
= isl_basic_set_dim(bset
, isl_dim_set
);
272 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
274 for (i
= 0; i
< n_div
; ++i
) {
275 if (isl_int_is_zero(c
[1 + nparam
+ d
+ i
]))
277 switch (div_purity
[i
]) {
278 case PURE_PARAM
: p
= 1; break;
279 case PURE_VAR
: v
= 1; break;
280 default: return IMPURE
;
283 if (!p
&& isl_seq_first_non_zero(c
+ 1, nparam
) == -1)
285 if (!v
&& isl_seq_first_non_zero(c
+ 1 + nparam
, d
) == -1)
290 bset
= isl_basic_set_copy(bset
);
291 bset
= isl_basic_set_cow(bset
);
292 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
293 k
= isl_basic_set_alloc_inequality(bset
);
296 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
297 isl_seq_cpy(bset
->ineq
[k
], c
, 1 + nparam
);
298 for (i
= 0; i
< n_div
; ++i
) {
299 if (div_purity
[i
] != PURE_PARAM
)
301 isl_int_set(bset
->ineq
[k
][1 + nparam
+ d
+ i
],
302 c
[1 + nparam
+ d
+ i
]);
304 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
305 empty
= isl_basic_set_is_empty(bset
);
306 isl_basic_set_free(bset
);
308 return empty
< 0 ? -1 : empty
? MIXED
: IMPURE
;
310 isl_basic_set_free(bset
);
314 /* Return an array of integers indicating the type of each div in bset.
315 * If the div is (recursively) defined in terms of only the parameters,
316 * then the type is PURE_PARAM.
317 * If the div is (recursively) defined in terms of only the set variables,
318 * then the type is PURE_VAR.
319 * Otherwise, the type is IMPURE.
321 static __isl_give
int *get_div_purity(__isl_keep isl_basic_set
*bset
)
332 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
333 d
= isl_basic_set_dim(bset
, isl_dim_set
);
334 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
336 div_purity
= isl_alloc_array(bset
->ctx
, int, n_div
);
340 for (i
= 0; i
< bset
->n_div
; ++i
) {
342 if (isl_int_is_zero(bset
->div
[i
][0])) {
343 div_purity
[i
] = IMPURE
;
346 if (isl_seq_first_non_zero(bset
->div
[i
] + 2, nparam
) != -1)
348 if (isl_seq_first_non_zero(bset
->div
[i
] + 2 + nparam
, d
) != -1)
350 for (j
= 0; j
< i
; ++j
) {
351 if (isl_int_is_zero(bset
->div
[i
][2 + nparam
+ d
+ j
]))
353 switch (div_purity
[j
]) {
354 case PURE_PARAM
: p
= 1; break;
355 case PURE_VAR
: v
= 1; break;
356 default: p
= v
= 1; break;
359 div_purity
[i
] = v
? p
? IMPURE
: PURE_VAR
: PURE_PARAM
;
365 /* Given a path with the as yet unconstrained length at position "pos",
366 * check if setting the length to zero results in only the identity
369 int empty_path_is_identity(__isl_keep isl_basic_map
*path
, unsigned pos
)
371 isl_basic_map
*test
= NULL
;
372 isl_basic_map
*id
= NULL
;
376 test
= isl_basic_map_copy(path
);
377 test
= isl_basic_map_extend_constraints(test
, 1, 0);
378 k
= isl_basic_map_alloc_equality(test
);
381 isl_seq_clr(test
->eq
[k
], 1 + isl_basic_map_total_dim(test
));
382 isl_int_set_si(test
->eq
[k
][pos
], 1);
383 id
= isl_basic_map_identity(isl_dim_domain(isl_basic_map_get_dim(path
)));
384 is_id
= isl_basic_map_is_subset(test
, id
);
385 isl_basic_map_free(test
);
386 isl_basic_map_free(id
);
389 isl_basic_map_free(test
);
393 __isl_give isl_basic_map
*add_delta_constraints(__isl_take isl_basic_map
*path
,
394 __isl_keep isl_basic_set
*delta
, unsigned off
, unsigned nparam
,
395 unsigned d
, int *div_purity
, int eq
)
398 int n
= eq
? delta
->n_eq
: delta
->n_ineq
;
399 isl_int
**delta_c
= eq
? delta
->eq
: delta
->ineq
;
400 isl_int
**path_c
= eq
? path
->eq
: path
->ineq
;
403 n_div
= isl_basic_set_dim(delta
, isl_dim_div
);
405 for (i
= 0; i
< n
; ++i
) {
406 int p
= purity(delta
, delta_c
[i
], div_purity
, eq
);
412 k
= isl_basic_map_alloc_equality(path
);
414 k
= isl_basic_map_alloc_inequality(path
);
417 isl_seq_clr(path_c
[k
], 1 + isl_basic_map_total_dim(path
));
419 isl_seq_cpy(path_c
[k
] + off
,
420 delta_c
[i
] + 1 + nparam
, d
);
421 isl_int_set(path_c
[k
][off
+ d
], delta_c
[i
][0]);
422 } else if (p
== PURE_PARAM
) {
423 isl_seq_cpy(path_c
[k
], delta_c
[i
], 1 + nparam
);
425 isl_seq_cpy(path_c
[k
] + off
,
426 delta_c
[i
] + 1 + nparam
, d
);
427 isl_seq_cpy(path_c
[k
], delta_c
[i
], 1 + nparam
);
429 isl_seq_cpy(path_c
[k
] + off
- n_div
,
430 delta_c
[i
] + 1 + nparam
+ d
, n_div
);
435 isl_basic_map_free(path
);
439 /* Given a set of offsets "delta", construct a relation of the
440 * given dimension specification (Z^{n+1} -> Z^{n+1}) that
441 * is an overapproximation of the relations that
442 * maps an element x to any element that can be reached
443 * by taking a non-negative number of steps along any of
444 * the elements in "delta".
445 * That is, construct an approximation of
447 * { [x] -> [y] : exists f \in \delta, k \in Z :
448 * y = x + k [f, 1] and k >= 0 }
450 * For any element in this relation, the number of steps taken
451 * is equal to the difference in the final coordinates.
453 * In particular, let delta be defined as
455 * \delta = [p] -> { [x] : A x + a >= and B p + b >= 0 and
456 * C x + C'p + c >= 0 and
457 * D x + D'p + d >= 0 }
459 * where the constraints C x + C'p + c >= 0 are such that the parametric
460 * constant term of each constraint j, "C_j x + C'_j p + c_j",
461 * can never attain positive values, then the relation is constructed as
463 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
464 * A f + k a >= 0 and B p + b >= 0 and
465 * C f + C'p + c >= 0 and k >= 1 }
466 * union { [x] -> [x] }
468 * If the zero-length paths happen to correspond exactly to the identity
469 * mapping, then we return
471 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
472 * A f + k a >= 0 and B p + b >= 0 and
473 * C f + C'p + c >= 0 and k >= 0 }
477 * Existentially quantified variables in \delta are handled by
478 * classifying them as independent of the parameters, purely
479 * parameter dependent and others. Constraints containing
480 * any of the other existentially quantified variables are removed.
481 * This is safe, but leads to an additional overapproximation.
483 static __isl_give isl_map
*path_along_delta(__isl_take isl_dim
*dim
,
484 __isl_take isl_basic_set
*delta
)
486 isl_basic_map
*path
= NULL
;
493 int *div_purity
= NULL
;
497 n_div
= isl_basic_set_dim(delta
, isl_dim_div
);
498 d
= isl_basic_set_dim(delta
, isl_dim_set
);
499 nparam
= isl_basic_set_dim(delta
, isl_dim_param
);
500 path
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), n_div
+ d
+ 1,
501 d
+ 1 + delta
->n_eq
, delta
->n_ineq
+ 1);
502 off
= 1 + nparam
+ 2 * (d
+ 1) + n_div
;
504 for (i
= 0; i
< n_div
+ d
+ 1; ++i
) {
505 k
= isl_basic_map_alloc_div(path
);
508 isl_int_set_si(path
->div
[k
][0], 0);
511 for (i
= 0; i
< d
+ 1; ++i
) {
512 k
= isl_basic_map_alloc_equality(path
);
515 isl_seq_clr(path
->eq
[k
], 1 + isl_basic_map_total_dim(path
));
516 isl_int_set_si(path
->eq
[k
][1 + nparam
+ i
], 1);
517 isl_int_set_si(path
->eq
[k
][1 + nparam
+ d
+ 1 + i
], -1);
518 isl_int_set_si(path
->eq
[k
][off
+ i
], 1);
521 div_purity
= get_div_purity(delta
);
525 path
= add_delta_constraints(path
, delta
, off
, nparam
, d
, div_purity
, 1);
526 path
= add_delta_constraints(path
, delta
, off
, nparam
, d
, div_purity
, 0);
528 is_id
= empty_path_is_identity(path
, off
+ d
);
532 k
= isl_basic_map_alloc_inequality(path
);
535 isl_seq_clr(path
->ineq
[k
], 1 + isl_basic_map_total_dim(path
));
537 isl_int_set_si(path
->ineq
[k
][0], -1);
538 isl_int_set_si(path
->ineq
[k
][off
+ d
], 1);
541 isl_basic_set_free(delta
);
542 path
= isl_basic_map_finalize(path
);
545 return isl_map_from_basic_map(path
);
547 return isl_basic_map_union(path
,
548 isl_basic_map_identity(isl_dim_domain(dim
)));
552 isl_basic_set_free(delta
);
553 isl_basic_map_free(path
);
557 /* Given a dimenion specification Z^{n+1} -> Z^{n+1} and a parameter "param",
558 * construct a map that equates the parameter to the difference
559 * in the final coordinates and imposes that this difference is positive.
562 * { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
564 static __isl_give isl_map
*equate_parameter_to_length(__isl_take isl_dim
*dim
,
567 struct isl_basic_map
*bmap
;
572 d
= isl_dim_size(dim
, isl_dim_in
);
573 nparam
= isl_dim_size(dim
, isl_dim_param
);
574 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 1);
575 k
= isl_basic_map_alloc_equality(bmap
);
578 isl_seq_clr(bmap
->eq
[k
], 1 + isl_basic_map_total_dim(bmap
));
579 isl_int_set_si(bmap
->eq
[k
][1 + param
], -1);
580 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ d
- 1], -1);
581 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ d
+ d
- 1], 1);
583 k
= isl_basic_map_alloc_inequality(bmap
);
586 isl_seq_clr(bmap
->ineq
[k
], 1 + isl_basic_map_total_dim(bmap
));
587 isl_int_set_si(bmap
->ineq
[k
][1 + param
], 1);
588 isl_int_set_si(bmap
->ineq
[k
][0], -1);
590 bmap
= isl_basic_map_finalize(bmap
);
591 return isl_map_from_basic_map(bmap
);
593 isl_basic_map_free(bmap
);
597 /* Check whether "path" is acyclic, where the last coordinates of domain
598 * and range of path encode the number of steps taken.
599 * That is, check whether
601 * { d | d = y - x and (x,y) in path }
603 * does not contain any element with positive last coordinate (positive length)
604 * and zero remaining coordinates (cycle).
606 static int is_acyclic(__isl_take isl_map
*path
)
611 struct isl_set
*delta
;
613 delta
= isl_map_deltas(path
);
614 dim
= isl_set_dim(delta
, isl_dim_set
);
615 for (i
= 0; i
< dim
; ++i
) {
617 delta
= isl_set_lower_bound_si(delta
, isl_dim_set
, i
, 1);
619 delta
= isl_set_fix_si(delta
, isl_dim_set
, i
, 0);
622 acyclic
= isl_set_is_empty(delta
);
628 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
629 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
630 * construct a map that is an overapproximation of the map
631 * that takes an element from the space D \times Z to another
632 * element from the same space, such that the first n coordinates of the
633 * difference between them is a sum of differences between images
634 * and pre-images in one of the R_i and such that the last coordinate
635 * is equal to the number of steps taken.
638 * \Delta_i = { y - x | (x, y) in R_i }
640 * then the constructed map is an overapproximation of
642 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
643 * d = (\sum_i k_i \delta_i, \sum_i k_i) }
645 * The elements of the singleton \Delta_i's are collected as the
646 * rows of the steps matrix. For all these \Delta_i's together,
647 * a single path is constructed.
648 * For each of the other \Delta_i's, we compute an overapproximation
649 * of the paths along elements of \Delta_i.
650 * Since each of these paths performs an addition, composition is
651 * symmetric and we can simply compose all resulting paths in any order.
653 static __isl_give isl_map
*construct_extended_path(__isl_take isl_dim
*dim
,
654 __isl_keep isl_map
*map
, int *project
)
656 struct isl_mat
*steps
= NULL
;
657 struct isl_map
*path
= NULL
;
661 d
= isl_map_dim(map
, isl_dim_in
);
663 path
= isl_map_identity(isl_dim_domain(isl_dim_copy(dim
)));
665 steps
= isl_mat_alloc(map
->ctx
, map
->n
, d
);
670 for (i
= 0; i
< map
->n
; ++i
) {
671 struct isl_basic_set
*delta
;
673 delta
= isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
]));
675 for (j
= 0; j
< d
; ++j
) {
678 fixed
= isl_basic_set_fast_dim_is_fixed(delta
, j
,
681 isl_basic_set_free(delta
);
690 path
= isl_map_apply_range(path
,
691 path_along_delta(isl_dim_copy(dim
), delta
));
692 path
= isl_map_coalesce(path
);
694 isl_basic_set_free(delta
);
701 path
= isl_map_apply_range(path
,
702 path_along_steps(isl_dim_copy(dim
), steps
));
705 if (project
&& *project
) {
706 *project
= is_acyclic(isl_map_copy(path
));
721 static int isl_set_overlaps(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
726 i
= isl_set_intersect(isl_set_copy(set1
), isl_set_copy(set2
));
727 no_overlap
= isl_set_is_empty(i
);
730 return no_overlap
< 0 ? -1 : !no_overlap
;
733 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
734 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
735 * construct a map that is an overapproximation of the map
736 * that takes an element from the dom R \times Z to an
737 * element from ran R \times Z, such that the first n coordinates of the
738 * difference between them is a sum of differences between images
739 * and pre-images in one of the R_i and such that the last coordinate
740 * is equal to the number of steps taken.
743 * \Delta_i = { y - x | (x, y) in R_i }
745 * then the constructed map is an overapproximation of
747 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
748 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
749 * x in dom R and x + d in ran R and
752 static __isl_give isl_map
*construct_component(__isl_take isl_dim
*dim
,
753 __isl_keep isl_map
*map
, int *exact
, int project
)
755 struct isl_set
*domain
= NULL
;
756 struct isl_set
*range
= NULL
;
757 struct isl_map
*app
= NULL
;
758 struct isl_map
*path
= NULL
;
760 domain
= isl_map_domain(isl_map_copy(map
));
761 domain
= isl_set_coalesce(domain
);
762 range
= isl_map_range(isl_map_copy(map
));
763 range
= isl_set_coalesce(range
);
764 if (!isl_set_overlaps(domain
, range
)) {
765 isl_set_free(domain
);
769 map
= isl_map_copy(map
);
770 map
= isl_map_add(map
, isl_dim_in
, 1);
771 map
= isl_map_add(map
, isl_dim_out
, 1);
772 map
= set_path_length(map
, 1, 1);
775 app
= isl_map_from_domain_and_range(domain
, range
);
776 app
= isl_map_add(app
, isl_dim_in
, 1);
777 app
= isl_map_add(app
, isl_dim_out
, 1);
779 path
= construct_extended_path(isl_dim_copy(dim
), map
,
780 exact
&& *exact
? &project
: NULL
);
781 app
= isl_map_intersect(app
, path
);
783 if (exact
&& *exact
&&
784 (*exact
= check_exactness(isl_map_copy(map
), isl_map_copy(app
),
789 app
= set_path_length(app
, 0, 1);
797 /* Call construct_component and, if "project" is set, project out
798 * the final coordinates.
800 static __isl_give isl_map
*construct_projected_component(
801 __isl_take isl_dim
*dim
,
802 __isl_keep isl_map
*map
, int *exact
, int project
)
809 d
= isl_dim_size(dim
, isl_dim_in
);
811 app
= construct_component(dim
, map
, exact
, project
);
813 app
= isl_map_project_out(app
, isl_dim_in
, d
- 1, 1);
814 app
= isl_map_project_out(app
, isl_dim_out
, d
- 1, 1);
819 /* Given an array of sets "set", add "dom" at position "pos"
820 * and search for elements at earlier positions that overlap with "dom".
821 * If any can be found, then merge all of them, together with "dom", into
822 * a single set and assign the union to the first in the array,
823 * which becomes the new group leader for all groups involved in the merge.
824 * During the search, we only consider group leaders, i.e., those with
825 * group[i] = i, as the other sets have already been combined
826 * with one of the group leaders.
828 static int merge(isl_set
**set
, int *group
, __isl_take isl_set
*dom
, int pos
)
833 set
[pos
] = isl_set_copy(dom
);
835 for (i
= pos
- 1; i
>= 0; --i
) {
841 o
= isl_set_overlaps(set
[i
], dom
);
847 set
[i
] = isl_set_union(set
[i
], set
[group
[pos
]]);
850 set
[group
[pos
]] = NULL
;
851 group
[group
[pos
]] = i
;
862 /* Given a partition of the domains and ranges of the basic maps in "map",
863 * apply the Floyd-Warshall algorithm with the elements in the partition
866 * In particular, there are "n" elements in the partition and "group" is
867 * an array of length 2 * map->n with entries in [0,n-1].
869 * We first construct a matrix of relations based on the partition information,
870 * apply Floyd-Warshall on this matrix of relations and then take the
871 * union of all entries in the matrix as the final result.
873 * The algorithm iterates over all vertices. In each step, the whole
874 * matrix is updated to include all paths that go to the current vertex,
875 * possibly stay there a while (including passing through earlier vertices)
876 * and then come back. At the start of each iteration, the diagonal
877 * element corresponding to the current vertex is replaced by its
878 * transitive closure to account for all indirect paths that stay
879 * in the current vertex.
881 static __isl_give isl_map
*floyd_warshall_with_groups(__isl_take isl_dim
*dim
,
882 __isl_keep isl_map
*map
, int *exact
, int project
, int *group
, int n
)
886 isl_map
***grid
= NULL
;
894 return construct_projected_component(dim
, map
, exact
, project
);
897 grid
= isl_calloc_array(map
->ctx
, isl_map
**, n
);
900 for (i
= 0; i
< n
; ++i
) {
901 grid
[i
] = isl_calloc_array(map
->ctx
, isl_map
*, n
);
904 for (j
= 0; j
< n
; ++j
)
905 grid
[i
][j
] = isl_map_empty(isl_map_get_dim(map
));
908 for (k
= 0; k
< map
->n
; ++k
) {
910 j
= group
[2 * k
+ 1];
911 grid
[i
][j
] = isl_map_union(grid
[i
][j
],
912 isl_map_from_basic_map(
913 isl_basic_map_copy(map
->p
[k
])));
916 for (r
= 0; r
< n
; ++r
) {
918 grid
[r
][r
] = isl_map_transitive_closure(grid
[r
][r
],
919 (exact
&& *exact
) ? &r_exact
: NULL
);
920 if (exact
&& *exact
&& !r_exact
)
923 for (p
= 0; p
< n
; ++p
)
924 for (q
= 0; q
< n
; ++q
) {
926 if (p
== r
&& q
== r
)
928 loop
= isl_map_apply_range(
929 isl_map_copy(grid
[p
][r
]),
930 isl_map_copy(grid
[r
][q
]));
931 grid
[p
][q
] = isl_map_union(grid
[p
][q
], loop
);
932 loop
= isl_map_apply_range(
933 isl_map_copy(grid
[p
][r
]),
935 isl_map_copy(grid
[r
][r
]),
936 isl_map_copy(grid
[r
][q
])));
937 grid
[p
][q
] = isl_map_union(grid
[p
][q
], loop
);
938 grid
[p
][q
] = isl_map_coalesce(grid
[p
][q
]);
942 app
= isl_map_empty(isl_map_get_dim(map
));
944 for (i
= 0; i
< n
; ++i
) {
945 for (j
= 0; j
< n
; ++j
)
946 app
= isl_map_union(app
, grid
[i
][j
]);
957 for (i
= 0; i
< n
; ++i
) {
960 for (j
= 0; j
< n
; ++j
)
961 isl_map_free(grid
[i
][j
]);
970 /* Check if the domains and ranges of the basic maps in "map" can
971 * be partitioned, and if so, apply Floyd-Warshall on the elements
972 * of the partition. Note that we can only apply this algorithm
973 * if we want to compute the transitive closure, i.e., when "project"
974 * is set. If we want to compute the power, we need to keep track
975 * of the lengths and the recursive calls inside the Floyd-Warshall
976 * would result in non-linear lengths.
978 * To find the partition, we simply consider all of the domains
979 * and ranges in turn and combine those that overlap.
980 * "set" contains the partition elements and "group" indicates
981 * to which partition element a given domain or range belongs.
982 * The domain of basic map i corresponds to element 2 * i in these arrays,
983 * while the domain corresponds to element 2 * i + 1.
984 * During the construction group[k] is either equal to k,
985 * in which case set[k] contains the union of all the domains and
986 * ranges in the corresponding group, or is equal to some l < k,
987 * with l another domain or range in the same group.
989 static __isl_give isl_map
*floyd_warshall(__isl_take isl_dim
*dim
,
990 __isl_keep isl_map
*map
, int *exact
, int project
)
993 isl_set
**set
= NULL
;
999 if (!project
|| map
->n
<= 1)
1000 return construct_projected_component(dim
, map
, exact
, project
);
1002 set
= isl_calloc_array(map
->ctx
, isl_set
*, 2 * map
->n
);
1003 group
= isl_alloc_array(map
->ctx
, int, 2 * map
->n
);
1008 for (i
= 0; i
< map
->n
; ++i
) {
1010 dom
= isl_set_from_basic_set(isl_basic_map_domain(
1011 isl_basic_map_copy(map
->p
[i
])));
1012 if (merge(set
, group
, dom
, 2 * i
) < 0)
1014 dom
= isl_set_from_basic_set(isl_basic_map_range(
1015 isl_basic_map_copy(map
->p
[i
])));
1016 if (merge(set
, group
, dom
, 2 * i
+ 1) < 0)
1021 for (i
= 0; i
< 2 * map
->n
; ++i
)
1025 group
[i
] = group
[group
[i
]];
1027 for (i
= 0; i
< 2 * map
->n
; ++i
)
1028 isl_set_free(set
[i
]);
1032 return floyd_warshall_with_groups(dim
, map
, exact
, project
, group
, n
);
1034 for (i
= 0; i
< 2 * map
->n
; ++i
)
1035 isl_set_free(set
[i
]);
1042 /* Structure for representing the nodes in the graph being traversed
1043 * using Tarjan's algorithm.
1044 * index represents the order in which nodes are visited.
1045 * min_index is the index of the root of a (sub)component.
1046 * on_stack indicates whether the node is currently on the stack.
1048 struct basic_map_sort_node
{
1053 /* Structure for representing the graph being traversed
1054 * using Tarjan's algorithm.
1055 * len is the number of nodes
1056 * node is an array of nodes
1057 * stack contains the nodes on the path from the root to the current node
1058 * sp is the stack pointer
1059 * index is the index of the last node visited
1060 * order contains the elements of the components separated by -1
1061 * op represents the current position in order
1063 struct basic_map_sort
{
1065 struct basic_map_sort_node
*node
;
1073 static void basic_map_sort_free(struct basic_map_sort
*s
)
1083 static struct basic_map_sort
*basic_map_sort_alloc(struct isl_ctx
*ctx
, int len
)
1085 struct basic_map_sort
*s
;
1088 s
= isl_calloc_type(ctx
, struct basic_map_sort
);
1092 s
->node
= isl_alloc_array(ctx
, struct basic_map_sort_node
, len
);
1095 for (i
= 0; i
< len
; ++i
)
1096 s
->node
[i
].index
= -1;
1097 s
->stack
= isl_alloc_array(ctx
, int, len
);
1100 s
->order
= isl_alloc_array(ctx
, int, 2 * len
);
1110 basic_map_sort_free(s
);
1114 /* Check whether in the computation of the transitive closure
1115 * "bmap1" (R_1) should follow (or be part of the same component as)
1118 * That is check whether
1126 * If so, then there is no reason for R_1 to immediately follow R_2
1129 static int basic_map_follows(__isl_keep isl_basic_map
*bmap1
,
1130 __isl_keep isl_basic_map
*bmap2
)
1132 struct isl_map
*map12
= NULL
;
1133 struct isl_map
*map21
= NULL
;
1136 map21
= isl_map_from_basic_map(
1137 isl_basic_map_apply_range(
1138 isl_basic_map_copy(bmap2
),
1139 isl_basic_map_copy(bmap1
)));
1140 subset
= isl_map_is_empty(map21
);
1144 isl_map_free(map21
);
1148 map12
= isl_map_from_basic_map(
1149 isl_basic_map_apply_range(
1150 isl_basic_map_copy(bmap1
),
1151 isl_basic_map_copy(bmap2
)));
1153 subset
= isl_map_is_subset(map21
, map12
);
1155 isl_map_free(map12
);
1156 isl_map_free(map21
);
1158 return subset
< 0 ? -1 : !subset
;
1160 isl_map_free(map21
);
1164 /* Perform Tarjan's algorithm for computing the strongly connected components
1165 * in the graph with the disjuncts of "map" as vertices and with an
1166 * edge between any pair of disjuncts such that the first has
1167 * to be applied after the second.
1169 static int power_components_tarjan(struct basic_map_sort
*s
,
1170 __isl_keep isl_map
*map
, int i
)
1174 s
->node
[i
].index
= s
->index
;
1175 s
->node
[i
].min_index
= s
->index
;
1176 s
->node
[i
].on_stack
= 1;
1178 s
->stack
[s
->sp
++] = i
;
1180 for (j
= s
->len
- 1; j
>= 0; --j
) {
1185 if (s
->node
[j
].index
>= 0 &&
1186 (!s
->node
[j
].on_stack
||
1187 s
->node
[j
].index
> s
->node
[i
].min_index
))
1190 f
= basic_map_follows(map
->p
[i
], map
->p
[j
]);
1196 if (s
->node
[j
].index
< 0) {
1197 power_components_tarjan(s
, map
, j
);
1198 if (s
->node
[j
].min_index
< s
->node
[i
].min_index
)
1199 s
->node
[i
].min_index
= s
->node
[j
].min_index
;
1200 } else if (s
->node
[j
].index
< s
->node
[i
].min_index
)
1201 s
->node
[i
].min_index
= s
->node
[j
].index
;
1204 if (s
->node
[i
].index
!= s
->node
[i
].min_index
)
1208 j
= s
->stack
[--s
->sp
];
1209 s
->node
[j
].on_stack
= 0;
1210 s
->order
[s
->op
++] = j
;
1212 s
->order
[s
->op
++] = -1;
1217 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1218 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1219 * construct a map that is an overapproximation of the map
1220 * that takes an element from the dom R \times Z to an
1221 * element from ran R \times Z, such that the first n coordinates of the
1222 * difference between them is a sum of differences between images
1223 * and pre-images in one of the R_i and such that the last coordinate
1224 * is equal to the number of steps taken.
1225 * If "project" is set, then these final coordinates are not included,
1226 * i.e., a relation of type Z^n -> Z^n is returned.
1229 * \Delta_i = { y - x | (x, y) in R_i }
1231 * then the constructed map is an overapproximation of
1233 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1234 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
1235 * x in dom R and x + d in ran R }
1239 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1240 * d = (\sum_i k_i \delta_i) and
1241 * x in dom R and x + d in ran R }
1243 * if "project" is set.
1245 * We first split the map into strongly connected components, perform
1246 * the above on each component and then join the results in the correct
1247 * order, at each join also taking in the union of both arguments
1248 * to allow for paths that do not go through one of the two arguments.
1250 static __isl_give isl_map
*construct_power_components(__isl_take isl_dim
*dim
,
1251 __isl_keep isl_map
*map
, int *exact
, int project
)
1254 struct isl_map
*path
= NULL
;
1255 struct basic_map_sort
*s
= NULL
;
1260 return floyd_warshall(dim
, map
, exact
, project
);
1262 s
= basic_map_sort_alloc(map
->ctx
, map
->n
);
1265 for (i
= map
->n
- 1; i
>= 0; --i
) {
1266 if (s
->node
[i
].index
>= 0)
1268 if (power_components_tarjan(s
, map
, i
) < 0)
1275 path
= isl_map_empty(isl_map_get_dim(map
));
1277 path
= isl_map_empty(isl_dim_copy(dim
));
1279 struct isl_map
*comp
;
1280 isl_map
*path_comp
, *path_comb
;
1281 comp
= isl_map_alloc_dim(isl_map_get_dim(map
), n
, 0);
1282 while (s
->order
[i
] != -1) {
1283 comp
= isl_map_add_basic_map(comp
,
1284 isl_basic_map_copy(map
->p
[s
->order
[i
]]));
1288 path_comp
= floyd_warshall(isl_dim_copy(dim
),
1289 comp
, exact
, project
);
1290 path_comb
= isl_map_apply_range(isl_map_copy(path
),
1291 isl_map_copy(path_comp
));
1292 path
= isl_map_union(path
, path_comp
);
1293 path
= isl_map_union(path
, path_comb
);
1298 basic_map_sort_free(s
);
1303 basic_map_sort_free(s
);
1308 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1309 * construct a map that is an overapproximation of the map
1310 * that takes an element from the space D to another
1311 * element from the same space, such that the difference between
1312 * them is a strictly positive sum of differences between images
1313 * and pre-images in one of the R_i.
1314 * The number of differences in the sum is equated to parameter "param".
1317 * \Delta_i = { y - x | (x, y) in R_i }
1319 * then the constructed map is an overapproximation of
1321 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1322 * d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1325 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1326 * d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1328 * if "project" is set.
1330 * If "project" is not set, then
1331 * we first construct an extended mapping with an extra coordinate
1332 * that indicates the number of steps taken. In particular,
1333 * the difference in the last coordinate is equal to the number
1334 * of steps taken to move from a domain element to the corresponding
1336 * In the final step, this difference is equated to the parameter "param"
1337 * and made positive. The extra coordinates are subsequently projected out.
1339 static __isl_give isl_map
*construct_power(__isl_keep isl_map
*map
,
1340 unsigned param
, int *exact
, int project
)
1342 struct isl_map
*app
= NULL
;
1343 struct isl_map
*diff
;
1344 struct isl_dim
*dim
= NULL
;
1350 dim
= isl_map_get_dim(map
);
1352 d
= isl_dim_size(dim
, isl_dim_in
);
1353 dim
= isl_dim_add(dim
, isl_dim_in
, 1);
1354 dim
= isl_dim_add(dim
, isl_dim_out
, 1);
1356 app
= construct_power_components(isl_dim_copy(dim
), map
,
1362 diff
= equate_parameter_to_length(dim
, param
);
1363 app
= isl_map_intersect(app
, diff
);
1364 app
= isl_map_project_out(app
, isl_dim_in
, d
, 1);
1365 app
= isl_map_project_out(app
, isl_dim_out
, d
, 1);
1371 /* Compute the positive powers of "map", or an overapproximation.
1372 * The power is given by parameter "param". If the result is exact,
1373 * then *exact is set to 1.
1375 * If project is set, then we are actually interested in the transitive
1376 * closure, so we can use a more relaxed exactness check.
1377 * The lengths of the paths are also projected out instead of being
1378 * equated to "param" (which is then ignored in this case).
1380 static __isl_give isl_map
*map_power(__isl_take isl_map
*map
, unsigned param
,
1381 int *exact
, int project
)
1383 struct isl_map
*app
= NULL
;
1388 map
= isl_map_coalesce(map
);
1392 if (isl_map_fast_is_empty(map
))
1395 isl_assert(map
->ctx
, project
|| param
< isl_map_dim(map
, isl_dim_param
),
1397 isl_assert(map
->ctx
,
1398 isl_map_dim(map
, isl_dim_in
) == isl_map_dim(map
, isl_dim_out
),
1401 app
= construct_power(map
, param
, exact
, project
);
1411 /* Compute the positive powers of "map", or an overapproximation.
1412 * The power is given by parameter "param". If the result is exact,
1413 * then *exact is set to 1.
1415 __isl_give isl_map
*isl_map_power(__isl_take isl_map
*map
, unsigned param
,
1418 return map_power(map
, param
, exact
, 0);
1421 /* Check whether equality i of bset is a pure stride constraint
1422 * on a single dimensions, i.e., of the form
1426 * with k a constant and e an existentially quantified variable.
1428 static int is_eq_stride(__isl_keep isl_basic_set
*bset
, int i
)
1440 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1443 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1444 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1445 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1447 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1449 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1452 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1453 d
- pos1
- 1) != -1)
1456 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1459 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1460 n_div
- pos2
- 1) != -1)
1462 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1463 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1469 /* Given a map, compute the smallest superset of this map that is of the form
1471 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
1473 * (where p ranges over the (non-parametric) dimensions),
1474 * compute the transitive closure of this map, i.e.,
1476 * { i -> j : exists k > 0:
1477 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
1479 * and intersect domain and range of this transitive closure with
1480 * domain and range of the original map.
1482 * If with_id is set, then try to include as much of the identity mapping
1483 * as possible, by computing
1485 * { i -> j : exists k >= 0:
1486 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
1488 * instead (i.e., allow k = 0) and by intersecting domain and range
1489 * with the union of the domain and the range of the original map.
1491 * In practice, we compute the difference set
1493 * delta = { j - i | i -> j in map },
1495 * look for stride constraint on the individual dimensions and compute
1496 * (constant) lower and upper bounds for each individual dimension,
1497 * adding a constraint for each bound not equal to infinity.
1499 static __isl_give isl_map
*box_closure(__isl_take isl_map
*map
, int with_id
)
1508 isl_set
*domain
= NULL
;
1509 isl_set
*range
= NULL
;
1510 isl_map
*app
= NULL
;
1511 isl_basic_set
*aff
= NULL
;
1512 isl_basic_map
*bmap
= NULL
;
1513 isl_vec
*obj
= NULL
;
1518 delta
= isl_map_deltas(isl_map_copy(map
));
1520 aff
= isl_set_affine_hull(isl_set_copy(delta
));
1523 dim
= isl_map_get_dim(map
);
1524 d
= isl_dim_size(dim
, isl_dim_in
);
1525 nparam
= isl_dim_size(dim
, isl_dim_param
);
1526 total
= isl_dim_total(dim
);
1527 bmap
= isl_basic_map_alloc_dim(dim
,
1528 aff
->n_div
+ 1, aff
->n_div
, 2 * d
+ 1);
1529 for (i
= 0; i
< aff
->n_div
+ 1; ++i
) {
1530 k
= isl_basic_map_alloc_div(bmap
);
1533 isl_int_set_si(bmap
->div
[k
][0], 0);
1535 for (i
= 0; i
< aff
->n_eq
; ++i
) {
1536 if (!is_eq_stride(aff
, i
))
1538 k
= isl_basic_map_alloc_equality(bmap
);
1541 isl_seq_clr(bmap
->eq
[k
], 1 + nparam
);
1542 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ d
,
1543 aff
->eq
[i
] + 1 + nparam
, d
);
1544 isl_seq_neg(bmap
->eq
[k
] + 1 + nparam
,
1545 aff
->eq
[i
] + 1 + nparam
, d
);
1546 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ 2 * d
,
1547 aff
->eq
[i
] + 1 + nparam
+ d
, aff
->n_div
);
1548 isl_int_set_si(bmap
->eq
[k
][1 + total
+ aff
->n_div
], 0);
1550 obj
= isl_vec_alloc(map
->ctx
, 1 + nparam
+ d
);
1553 isl_seq_clr(obj
->el
, 1 + nparam
+ d
);
1554 for (i
= 0; i
< d
; ++ i
) {
1555 enum isl_lp_result res
;
1557 isl_int_set_si(obj
->el
[1 + nparam
+ i
], 1);
1559 res
= isl_set_solve_lp(delta
, 0, obj
->el
, map
->ctx
->one
, &opt
,
1561 if (res
== isl_lp_error
)
1563 if (res
== isl_lp_ok
) {
1564 k
= isl_basic_map_alloc_inequality(bmap
);
1567 isl_seq_clr(bmap
->ineq
[k
],
1568 1 + nparam
+ 2 * d
+ bmap
->n_div
);
1569 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ i
], -1);
1570 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ d
+ i
], 1);
1571 isl_int_neg(bmap
->ineq
[k
][1 + nparam
+ 2 * d
+ aff
->n_div
], opt
);
1574 res
= isl_set_solve_lp(delta
, 1, obj
->el
, map
->ctx
->one
, &opt
,
1576 if (res
== isl_lp_error
)
1578 if (res
== isl_lp_ok
) {
1579 k
= isl_basic_map_alloc_inequality(bmap
);
1582 isl_seq_clr(bmap
->ineq
[k
],
1583 1 + nparam
+ 2 * d
+ bmap
->n_div
);
1584 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ i
], 1);
1585 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ d
+ i
], -1);
1586 isl_int_set(bmap
->ineq
[k
][1 + nparam
+ 2 * d
+ aff
->n_div
], opt
);
1589 isl_int_set_si(obj
->el
[1 + nparam
+ i
], 0);
1591 k
= isl_basic_map_alloc_inequality(bmap
);
1594 isl_seq_clr(bmap
->ineq
[k
],
1595 1 + nparam
+ 2 * d
+ bmap
->n_div
);
1597 isl_int_set_si(bmap
->ineq
[k
][0], -1);
1598 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ 2 * d
+ aff
->n_div
], 1);
1600 domain
= isl_map_domain(isl_map_copy(map
));
1601 domain
= isl_set_coalesce(domain
);
1602 range
= isl_map_range(isl_map_copy(map
));
1603 range
= isl_set_coalesce(range
);
1605 domain
= isl_set_union(domain
, range
);
1606 domain
= isl_set_coalesce(domain
);
1607 range
= isl_set_copy(domain
);
1609 app
= isl_map_from_domain_and_range(domain
, range
);
1612 isl_basic_set_free(aff
);
1614 bmap
= isl_basic_map_finalize(bmap
);
1615 isl_set_free(delta
);
1618 map
= isl_map_from_basic_map(bmap
);
1619 map
= isl_map_intersect(map
, app
);
1624 isl_basic_map_free(bmap
);
1625 isl_basic_set_free(aff
);
1627 isl_set_free(delta
);
1632 /* Check whether app is the transitive closure of map.
1633 * In particular, check that app is acyclic and, if so,
1636 * app \subset (map \cup (map \circ app))
1638 static int check_exactness_omega(__isl_keep isl_map
*map
,
1639 __isl_keep isl_map
*app
)
1643 int is_empty
, is_exact
;
1647 delta
= isl_map_deltas(isl_map_copy(app
));
1648 d
= isl_set_dim(delta
, isl_dim_set
);
1649 for (i
= 0; i
< d
; ++i
)
1650 delta
= isl_set_fix_si(delta
, isl_dim_set
, i
, 0);
1651 is_empty
= isl_set_is_empty(delta
);
1652 isl_set_free(delta
);
1658 test
= isl_map_apply_range(isl_map_copy(app
), isl_map_copy(map
));
1659 test
= isl_map_union(test
, isl_map_copy(map
));
1660 is_exact
= isl_map_is_subset(app
, test
);
1666 /* Check if basic map M_i can be combined with all the other
1667 * basic maps such that
1671 * can be computed as
1673 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
1675 * In particular, check if we can compute a compact representation
1678 * M_i^* \circ M_j \circ M_i^*
1681 * Let M_i^? be an extension of M_i^+ that allows paths
1682 * of length zero, i.e., the result of box_closure(., 1).
1683 * The criterion, as proposed by Kelly et al., is that
1684 * id = M_i^? - M_i^+ can be represented as a basic map
1687 * id \circ M_j \circ id = M_j
1691 * If this function returns 1, then tc and qc are set to
1692 * M_i^+ and M_i^?, respectively.
1694 static int can_be_split_off(__isl_keep isl_map
*map
, int i
,
1695 __isl_give isl_map
**tc
, __isl_give isl_map
**qc
)
1697 isl_map
*map_i
, *id
;
1700 map_i
= isl_map_from_basic_map(isl_basic_map_copy(map
->p
[i
]));
1701 *tc
= box_closure(isl_map_copy(map_i
), 0);
1702 *qc
= box_closure(map_i
, 1);
1703 id
= isl_map_subtract(isl_map_copy(*qc
), isl_map_copy(*tc
));
1707 if (id
->n
!= 1 || (*qc
)->n
!= 1)
1710 for (j
= 0; j
< map
->n
; ++j
) {
1711 isl_map
*map_j
, *test
;
1716 map_j
= isl_map_from_basic_map(
1717 isl_basic_map_copy(map
->p
[j
]));
1718 test
= isl_map_apply_range(isl_map_copy(id
),
1719 isl_map_copy(map_j
));
1720 test
= isl_map_apply_range(test
, isl_map_copy(id
));
1721 is_ok
= isl_map_is_equal(test
, map_j
);
1722 isl_map_free(map_j
);
1750 /* Compute an overapproximation of the transitive closure of "map"
1751 * using a variation of the algorithm from
1752 * "Transitive Closure of Infinite Graphs and its Applications"
1755 * We first check whether we can can split of any basic map M_i and
1762 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
1764 * using a recursive call on the remaining map.
1766 * If not, we simply call box_closure on the whole map.
1768 static __isl_give isl_map
*compute_closure_omega(__isl_take isl_map
*map
)
1775 return box_closure(map
, 0);
1777 map
= isl_map_cow(map
);
1781 for (i
= 0; i
< map
->n
; ++i
) {
1784 ok
= can_be_split_off(map
, i
, &tc
, &qc
);
1790 isl_basic_map_free(map
->p
[i
]);
1791 if (i
!= map
->n
- 1)
1792 map
->p
[i
] = map
->p
[map
->n
- 1];
1795 map
= isl_map_apply_range(isl_map_copy(qc
), map
);
1796 map
= isl_map_apply_range(map
, qc
);
1798 return isl_map_union(tc
, compute_closure_omega(map
));
1801 return box_closure(map
, 0);
1807 /* Compute an overapproximation of the transitive closure of "map"
1808 * using a variation of the algorithm from
1809 * "Transitive Closure of Infinite Graphs and its Applications"
1810 * by Kelly et al. and check whether the result is definitely exact.
1812 static __isl_give isl_map
*transitive_closure_omega(__isl_take isl_map
*map
,
1817 app
= compute_closure_omega(isl_map_copy(map
));
1820 *exact
= check_exactness_omega(map
, app
);
1826 /* Compute the transitive closure of "map", or an overapproximation.
1827 * If the result is exact, then *exact is set to 1.
1828 * Simply use map_power to compute the powers of map, but tell
1829 * it to project out the lengths of the paths instead of equating
1830 * the length to a parameter.
1832 __isl_give isl_map
*isl_map_transitive_closure(__isl_take isl_map
*map
,
1840 if (map
->ctx
->opt
->closure
== ISL_CLOSURE_OMEGA
)
1841 return transitive_closure_omega(map
, exact
);
1843 param
= isl_map_dim(map
, isl_dim_param
);
1844 map
= map_power(map
, param
, exact
, 1);