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 int isl_map_is_transitively_closed(__isl_keep isl_map
*map
)
21 map2
= isl_map_apply_range(isl_map_copy(map
), isl_map_copy(map
));
22 closed
= isl_map_is_subset(map2
, map
);
28 /* Given a map that represents a path with the length of the path
29 * encoded as the difference between the last output coordindate
30 * and the last input coordinate, set this length to either
31 * exactly "length" (if "exactly" is set) or at least "length"
32 * (if "exactly" is not set).
34 static __isl_give isl_map
*set_path_length(__isl_take isl_map
*map
,
35 int exactly
, int length
)
38 struct isl_basic_map
*bmap
;
47 dim
= isl_map_get_dim(map
);
48 d
= isl_dim_size(dim
, isl_dim_in
);
49 nparam
= isl_dim_size(dim
, isl_dim_param
);
50 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 1);
52 k
= isl_basic_map_alloc_equality(bmap
);
55 k
= isl_basic_map_alloc_inequality(bmap
);
60 isl_seq_clr(c
, 1 + isl_basic_map_total_dim(bmap
));
61 isl_int_set_si(c
[0], -length
);
62 isl_int_set_si(c
[1 + nparam
+ d
- 1], -1);
63 isl_int_set_si(c
[1 + nparam
+ d
+ d
- 1], 1);
65 bmap
= isl_basic_map_finalize(bmap
);
66 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
70 isl_basic_map_free(bmap
);
75 /* Check whether the overapproximation of the power of "map" is exactly
76 * the power of "map". Let R be "map" and A_k the overapproximation.
77 * The approximation is exact if
80 * A_k = A_{k-1} \circ R k >= 2
82 * Since A_k is known to be an overapproximation, we only need to check
85 * A_k \subset A_{k-1} \circ R k >= 2
87 * In practice, "app" has an extra input and output coordinate
88 * to encode the length of the path. So, we first need to add
89 * this coordinate to "map" and set the length of the path to
92 static int check_power_exactness(__isl_take isl_map
*map
,
93 __isl_take isl_map
*app
)
99 map
= isl_map_add(map
, isl_dim_in
, 1);
100 map
= isl_map_add(map
, isl_dim_out
, 1);
101 map
= set_path_length(map
, 1, 1);
103 app_1
= set_path_length(isl_map_copy(app
), 1, 1);
105 exact
= isl_map_is_subset(app_1
, map
);
108 if (!exact
|| exact
< 0) {
114 app_1
= set_path_length(isl_map_copy(app
), 0, 1);
115 app_2
= set_path_length(app
, 0, 2);
116 app_1
= isl_map_apply_range(map
, app_1
);
118 exact
= isl_map_is_subset(app_2
, app_1
);
126 /* Check whether the overapproximation of the power of "map" is exactly
127 * the power of "map", possibly after projecting out the power (if "project"
130 * If "project" is set and if "steps" can only result in acyclic paths,
133 * A = R \cup (A \circ R)
135 * where A is the overapproximation with the power projected out, i.e.,
136 * an overapproximation of the transitive closure.
137 * More specifically, since A is known to be an overapproximation, we check
139 * A \subset R \cup (A \circ R)
141 * Otherwise, we check if the power is exact.
143 * Note that "app" has an extra input and output coordinate to encode
144 * the length of the part. If we are only interested in the transitive
145 * closure, then we can simply project out these coordinates first.
147 static int check_exactness(__isl_take isl_map
*map
, __isl_take isl_map
*app
,
155 return check_power_exactness(map
, app
);
157 d
= isl_map_dim(map
, isl_dim_in
);
158 app
= set_path_length(app
, 0, 1);
159 app
= isl_map_project_out(app
, isl_dim_in
, d
, 1);
160 app
= isl_map_project_out(app
, isl_dim_out
, d
, 1);
162 test
= isl_map_apply_range(isl_map_copy(map
), isl_map_copy(app
));
163 test
= isl_map_union(test
, isl_map_copy(map
));
165 exact
= isl_map_is_subset(app
, test
);
176 * The transitive closure implementation is based on the paper
177 * "Computing the Transitive Closure of a Union of Affine Integer
178 * Tuple Relations" by Anna Beletska, Denis Barthou, Wlodzimierz Bielecki and
182 /* Given a set of n offsets v_i (the rows of "steps"), construct a relation
183 * of the given dimension specification (Z^{n+1} -> Z^{n+1})
184 * that maps an element x to any element that can be reached
185 * by taking a non-negative number of steps along any of
186 * the extended offsets v'_i = [v_i 1].
189 * { [x] -> [y] : exists k_i >= 0, y = x + \sum_i k_i v'_i }
191 * For any element in this relation, the number of steps taken
192 * is equal to the difference in the final coordinates.
194 static __isl_give isl_map
*path_along_steps(__isl_take isl_dim
*dim
,
195 __isl_keep isl_mat
*steps
)
198 struct isl_basic_map
*path
= NULL
;
206 d
= isl_dim_size(dim
, isl_dim_in
);
208 nparam
= isl_dim_size(dim
, isl_dim_param
);
210 path
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), n
, d
, n
);
212 for (i
= 0; i
< n
; ++i
) {
213 k
= isl_basic_map_alloc_div(path
);
216 isl_assert(steps
->ctx
, i
== k
, goto error
);
217 isl_int_set_si(path
->div
[k
][0], 0);
220 for (i
= 0; i
< d
; ++i
) {
221 k
= isl_basic_map_alloc_equality(path
);
224 isl_seq_clr(path
->eq
[k
], 1 + isl_basic_map_total_dim(path
));
225 isl_int_set_si(path
->eq
[k
][1 + nparam
+ i
], 1);
226 isl_int_set_si(path
->eq
[k
][1 + nparam
+ d
+ i
], -1);
228 for (j
= 0; j
< n
; ++j
)
229 isl_int_set_si(path
->eq
[k
][1 + nparam
+ 2 * d
+ j
], 1);
231 for (j
= 0; j
< n
; ++j
)
232 isl_int_set(path
->eq
[k
][1 + nparam
+ 2 * d
+ j
],
236 for (i
= 0; i
< n
; ++i
) {
237 k
= isl_basic_map_alloc_inequality(path
);
240 isl_seq_clr(path
->ineq
[k
], 1 + isl_basic_map_total_dim(path
));
241 isl_int_set_si(path
->ineq
[k
][1 + nparam
+ 2 * d
+ i
], 1);
246 path
= isl_basic_map_simplify(path
);
247 path
= isl_basic_map_finalize(path
);
248 return isl_map_from_basic_map(path
);
251 isl_basic_map_free(path
);
260 /* Check whether the parametric constant term of constraint c is never
261 * positive in "bset".
263 static int parametric_constant_never_positive(__isl_keep isl_basic_set
*bset
,
264 isl_int
*c
, int *div_purity
)
273 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
274 d
= isl_basic_set_dim(bset
, isl_dim_set
);
275 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
277 bset
= isl_basic_set_copy(bset
);
278 bset
= isl_basic_set_cow(bset
);
279 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
280 k
= isl_basic_set_alloc_inequality(bset
);
283 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
284 isl_seq_cpy(bset
->ineq
[k
], c
, 1 + nparam
);
285 for (i
= 0; i
< n_div
; ++i
) {
286 if (div_purity
[i
] != PURE_PARAM
)
288 isl_int_set(bset
->ineq
[k
][1 + nparam
+ d
+ i
],
289 c
[1 + nparam
+ d
+ i
]);
291 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
292 empty
= isl_basic_set_is_empty(bset
);
293 isl_basic_set_free(bset
);
297 isl_basic_set_free(bset
);
301 /* Return PURE_PARAM if only the coefficients of the parameters are non-zero.
302 * Return PURE_VAR if only the coefficients of the set variables are non-zero.
303 * Return MIXED if only the coefficients of the parameters and the set
304 * variables are non-zero and if moreover the parametric constant
305 * can never attain positive values.
306 * Return IMPURE otherwise.
308 static int purity(__isl_keep isl_basic_set
*bset
, isl_int
*c
, int *div_purity
,
318 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
319 d
= isl_basic_set_dim(bset
, isl_dim_set
);
320 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
322 for (i
= 0; i
< n_div
; ++i
) {
323 if (isl_int_is_zero(c
[1 + nparam
+ d
+ i
]))
325 switch (div_purity
[i
]) {
326 case PURE_PARAM
: p
= 1; break;
327 case PURE_VAR
: v
= 1; break;
328 default: return IMPURE
;
331 if (!p
&& isl_seq_first_non_zero(c
+ 1, nparam
) == -1)
333 if (!v
&& isl_seq_first_non_zero(c
+ 1 + nparam
, d
) == -1)
336 empty
= parametric_constant_never_positive(bset
, c
, div_purity
);
337 if (eq
&& empty
>= 0 && !empty
) {
338 isl_seq_neg(c
, c
, 1 + nparam
+ d
+ n_div
);
339 empty
= parametric_constant_never_positive(bset
, c
, div_purity
);
342 return empty
< 0 ? -1 : empty
? MIXED
: IMPURE
;
345 /* Return an array of integers indicating the type of each div in bset.
346 * If the div is (recursively) defined in terms of only the parameters,
347 * then the type is PURE_PARAM.
348 * If the div is (recursively) defined in terms of only the set variables,
349 * then the type is PURE_VAR.
350 * Otherwise, the type is IMPURE.
352 static __isl_give
int *get_div_purity(__isl_keep isl_basic_set
*bset
)
363 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
364 d
= isl_basic_set_dim(bset
, isl_dim_set
);
365 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
367 div_purity
= isl_alloc_array(bset
->ctx
, int, n_div
);
371 for (i
= 0; i
< bset
->n_div
; ++i
) {
373 if (isl_int_is_zero(bset
->div
[i
][0])) {
374 div_purity
[i
] = IMPURE
;
377 if (isl_seq_first_non_zero(bset
->div
[i
] + 2, nparam
) != -1)
379 if (isl_seq_first_non_zero(bset
->div
[i
] + 2 + nparam
, d
) != -1)
381 for (j
= 0; j
< i
; ++j
) {
382 if (isl_int_is_zero(bset
->div
[i
][2 + nparam
+ d
+ j
]))
384 switch (div_purity
[j
]) {
385 case PURE_PARAM
: p
= 1; break;
386 case PURE_VAR
: v
= 1; break;
387 default: p
= v
= 1; break;
390 div_purity
[i
] = v
? p
? IMPURE
: PURE_VAR
: PURE_PARAM
;
396 /* Given a path with the as yet unconstrained length at position "pos",
397 * check if setting the length to zero results in only the identity
400 int empty_path_is_identity(__isl_keep isl_basic_map
*path
, unsigned pos
)
402 isl_basic_map
*test
= NULL
;
403 isl_basic_map
*id
= NULL
;
407 test
= isl_basic_map_copy(path
);
408 test
= isl_basic_map_extend_constraints(test
, 1, 0);
409 k
= isl_basic_map_alloc_equality(test
);
412 isl_seq_clr(test
->eq
[k
], 1 + isl_basic_map_total_dim(test
));
413 isl_int_set_si(test
->eq
[k
][pos
], 1);
414 id
= isl_basic_map_identity(isl_dim_domain(isl_basic_map_get_dim(path
)));
415 is_id
= isl_basic_map_is_equal(test
, id
);
416 isl_basic_map_free(test
);
417 isl_basic_map_free(id
);
420 isl_basic_map_free(test
);
424 __isl_give isl_basic_map
*add_delta_constraints(__isl_take isl_basic_map
*path
,
425 __isl_keep isl_basic_set
*delta
, unsigned off
, unsigned nparam
,
426 unsigned d
, int *div_purity
, int eq
)
429 int n
= eq
? delta
->n_eq
: delta
->n_ineq
;
430 isl_int
**delta_c
= eq
? delta
->eq
: delta
->ineq
;
433 n_div
= isl_basic_set_dim(delta
, isl_dim_div
);
435 for (i
= 0; i
< n
; ++i
) {
437 int p
= purity(delta
, delta_c
[i
], div_purity
, eq
);
442 if (eq
&& p
!= MIXED
) {
443 k
= isl_basic_map_alloc_equality(path
);
444 path_c
= path
->eq
[k
];
446 k
= isl_basic_map_alloc_inequality(path
);
447 path_c
= path
->ineq
[k
];
451 isl_seq_clr(path_c
, 1 + isl_basic_map_total_dim(path
));
453 isl_seq_cpy(path_c
+ off
,
454 delta_c
[i
] + 1 + nparam
, d
);
455 isl_int_set(path_c
[off
+ d
], delta_c
[i
][0]);
456 } else if (p
== PURE_PARAM
) {
457 isl_seq_cpy(path_c
, delta_c
[i
], 1 + nparam
);
459 isl_seq_cpy(path_c
+ off
,
460 delta_c
[i
] + 1 + nparam
, d
);
461 isl_seq_cpy(path_c
, delta_c
[i
], 1 + nparam
);
463 isl_seq_cpy(path_c
+ off
- n_div
,
464 delta_c
[i
] + 1 + nparam
+ d
, n_div
);
469 isl_basic_map_free(path
);
473 /* Given a set of offsets "delta", construct a relation of the
474 * given dimension specification (Z^{n+1} -> Z^{n+1}) that
475 * is an overapproximation of the relations that
476 * maps an element x to any element that can be reached
477 * by taking a non-negative number of steps along any of
478 * the elements in "delta".
479 * That is, construct an approximation of
481 * { [x] -> [y] : exists f \in \delta, k \in Z :
482 * y = x + k [f, 1] and k >= 0 }
484 * For any element in this relation, the number of steps taken
485 * is equal to the difference in the final coordinates.
487 * In particular, let delta be defined as
489 * \delta = [p] -> { [x] : A x + a >= and B p + b >= 0 and
490 * C x + C'p + c >= 0 and
491 * D x + D'p + d >= 0 }
493 * where the constraints C x + C'p + c >= 0 are such that the parametric
494 * constant term of each constraint j, "C_j x + C'_j p + c_j",
495 * can never attain positive values, then the relation is constructed as
497 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
498 * A f + k a >= 0 and B p + b >= 0 and
499 * C f + C'p + c >= 0 and k >= 1 }
500 * union { [x] -> [x] }
502 * If the zero-length paths happen to correspond exactly to the identity
503 * mapping, then we return
505 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
506 * A f + k a >= 0 and B p + b >= 0 and
507 * C f + C'p + c >= 0 and k >= 0 }
511 * Existentially quantified variables in \delta are handled by
512 * classifying them as independent of the parameters, purely
513 * parameter dependent and others. Constraints containing
514 * any of the other existentially quantified variables are removed.
515 * This is safe, but leads to an additional overapproximation.
517 static __isl_give isl_map
*path_along_delta(__isl_take isl_dim
*dim
,
518 __isl_take isl_basic_set
*delta
)
520 isl_basic_map
*path
= NULL
;
527 int *div_purity
= NULL
;
531 n_div
= isl_basic_set_dim(delta
, isl_dim_div
);
532 d
= isl_basic_set_dim(delta
, isl_dim_set
);
533 nparam
= isl_basic_set_dim(delta
, isl_dim_param
);
534 path
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), n_div
+ d
+ 1,
535 d
+ 1 + delta
->n_eq
, delta
->n_eq
+ delta
->n_ineq
+ 1);
536 off
= 1 + nparam
+ 2 * (d
+ 1) + n_div
;
538 for (i
= 0; i
< n_div
+ d
+ 1; ++i
) {
539 k
= isl_basic_map_alloc_div(path
);
542 isl_int_set_si(path
->div
[k
][0], 0);
545 for (i
= 0; i
< d
+ 1; ++i
) {
546 k
= isl_basic_map_alloc_equality(path
);
549 isl_seq_clr(path
->eq
[k
], 1 + isl_basic_map_total_dim(path
));
550 isl_int_set_si(path
->eq
[k
][1 + nparam
+ i
], 1);
551 isl_int_set_si(path
->eq
[k
][1 + nparam
+ d
+ 1 + i
], -1);
552 isl_int_set_si(path
->eq
[k
][off
+ i
], 1);
555 div_purity
= get_div_purity(delta
);
559 path
= add_delta_constraints(path
, delta
, off
, nparam
, d
, div_purity
, 1);
560 path
= add_delta_constraints(path
, delta
, off
, nparam
, d
, div_purity
, 0);
562 is_id
= empty_path_is_identity(path
, off
+ d
);
566 k
= isl_basic_map_alloc_inequality(path
);
569 isl_seq_clr(path
->ineq
[k
], 1 + isl_basic_map_total_dim(path
));
571 isl_int_set_si(path
->ineq
[k
][0], -1);
572 isl_int_set_si(path
->ineq
[k
][off
+ d
], 1);
575 isl_basic_set_free(delta
);
576 path
= isl_basic_map_finalize(path
);
579 return isl_map_from_basic_map(path
);
581 return isl_basic_map_union(path
,
582 isl_basic_map_identity(isl_dim_domain(dim
)));
586 isl_basic_set_free(delta
);
587 isl_basic_map_free(path
);
591 /* Given a dimenion specification Z^{n+1} -> Z^{n+1} and a parameter "param",
592 * construct a map that equates the parameter to the difference
593 * in the final coordinates and imposes that this difference is positive.
596 * { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
598 static __isl_give isl_map
*equate_parameter_to_length(__isl_take isl_dim
*dim
,
601 struct isl_basic_map
*bmap
;
606 d
= isl_dim_size(dim
, isl_dim_in
);
607 nparam
= isl_dim_size(dim
, isl_dim_param
);
608 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 1);
609 k
= isl_basic_map_alloc_equality(bmap
);
612 isl_seq_clr(bmap
->eq
[k
], 1 + isl_basic_map_total_dim(bmap
));
613 isl_int_set_si(bmap
->eq
[k
][1 + param
], -1);
614 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ d
- 1], -1);
615 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ d
+ d
- 1], 1);
617 k
= isl_basic_map_alloc_inequality(bmap
);
620 isl_seq_clr(bmap
->ineq
[k
], 1 + isl_basic_map_total_dim(bmap
));
621 isl_int_set_si(bmap
->ineq
[k
][1 + param
], 1);
622 isl_int_set_si(bmap
->ineq
[k
][0], -1);
624 bmap
= isl_basic_map_finalize(bmap
);
625 return isl_map_from_basic_map(bmap
);
627 isl_basic_map_free(bmap
);
631 /* Check whether "path" is acyclic, where the last coordinates of domain
632 * and range of path encode the number of steps taken.
633 * That is, check whether
635 * { d | d = y - x and (x,y) in path }
637 * does not contain any element with positive last coordinate (positive length)
638 * and zero remaining coordinates (cycle).
640 static int is_acyclic(__isl_take isl_map
*path
)
645 struct isl_set
*delta
;
647 delta
= isl_map_deltas(path
);
648 dim
= isl_set_dim(delta
, isl_dim_set
);
649 for (i
= 0; i
< dim
; ++i
) {
651 delta
= isl_set_lower_bound_si(delta
, isl_dim_set
, i
, 1);
653 delta
= isl_set_fix_si(delta
, isl_dim_set
, i
, 0);
656 acyclic
= isl_set_is_empty(delta
);
662 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
663 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
664 * construct a map that is an overapproximation of the map
665 * that takes an element from the space D \times Z to another
666 * element from the same space, such that the first n coordinates of the
667 * difference between them is a sum of differences between images
668 * and pre-images in one of the R_i and such that the last coordinate
669 * is equal to the number of steps taken.
672 * \Delta_i = { y - x | (x, y) in R_i }
674 * then the constructed map is an overapproximation of
676 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
677 * d = (\sum_i k_i \delta_i, \sum_i k_i) }
679 * The elements of the singleton \Delta_i's are collected as the
680 * rows of the steps matrix. For all these \Delta_i's together,
681 * a single path is constructed.
682 * For each of the other \Delta_i's, we compute an overapproximation
683 * of the paths along elements of \Delta_i.
684 * Since each of these paths performs an addition, composition is
685 * symmetric and we can simply compose all resulting paths in any order.
687 static __isl_give isl_map
*construct_extended_path(__isl_take isl_dim
*dim
,
688 __isl_keep isl_map
*map
, int *project
)
690 struct isl_mat
*steps
= NULL
;
691 struct isl_map
*path
= NULL
;
695 d
= isl_map_dim(map
, isl_dim_in
);
697 path
= isl_map_identity(isl_dim_domain(isl_dim_copy(dim
)));
699 steps
= isl_mat_alloc(map
->ctx
, map
->n
, d
);
704 for (i
= 0; i
< map
->n
; ++i
) {
705 struct isl_basic_set
*delta
;
707 delta
= isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
]));
709 for (j
= 0; j
< d
; ++j
) {
712 fixed
= isl_basic_set_fast_dim_is_fixed(delta
, j
,
715 isl_basic_set_free(delta
);
724 path
= isl_map_apply_range(path
,
725 path_along_delta(isl_dim_copy(dim
), delta
));
726 path
= isl_map_coalesce(path
);
728 isl_basic_set_free(delta
);
735 path
= isl_map_apply_range(path
,
736 path_along_steps(isl_dim_copy(dim
), steps
));
739 if (project
&& *project
) {
740 *project
= is_acyclic(isl_map_copy(path
));
755 static int isl_set_overlaps(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
760 i
= isl_set_intersect(isl_set_copy(set1
), isl_set_copy(set2
));
761 no_overlap
= isl_set_is_empty(i
);
764 return no_overlap
< 0 ? -1 : !no_overlap
;
767 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
768 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
769 * construct a map that is an overapproximation of the map
770 * that takes an element from the dom R \times Z to an
771 * element from ran R \times Z, such that the first n coordinates of the
772 * difference between them is a sum of differences between images
773 * and pre-images in one of the R_i and such that the last coordinate
774 * is equal to the number of steps taken.
777 * \Delta_i = { y - x | (x, y) in R_i }
779 * then the constructed map is an overapproximation of
781 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
782 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
783 * x in dom R and x + d in ran R and
786 static __isl_give isl_map
*construct_component(__isl_take isl_dim
*dim
,
787 __isl_keep isl_map
*map
, int *exact
, int project
)
789 struct isl_set
*domain
= NULL
;
790 struct isl_set
*range
= NULL
;
791 struct isl_map
*app
= NULL
;
792 struct isl_map
*path
= NULL
;
794 domain
= isl_map_domain(isl_map_copy(map
));
795 domain
= isl_set_coalesce(domain
);
796 range
= isl_map_range(isl_map_copy(map
));
797 range
= isl_set_coalesce(range
);
798 if (!isl_set_overlaps(domain
, range
)) {
799 isl_set_free(domain
);
803 map
= isl_map_copy(map
);
804 map
= isl_map_add(map
, isl_dim_in
, 1);
805 map
= isl_map_add(map
, isl_dim_out
, 1);
806 map
= set_path_length(map
, 1, 1);
809 app
= isl_map_from_domain_and_range(domain
, range
);
810 app
= isl_map_add(app
, isl_dim_in
, 1);
811 app
= isl_map_add(app
, isl_dim_out
, 1);
813 path
= construct_extended_path(isl_dim_copy(dim
), map
,
814 exact
&& *exact
? &project
: NULL
);
815 app
= isl_map_intersect(app
, path
);
817 if (exact
&& *exact
&&
818 (*exact
= check_exactness(isl_map_copy(map
), isl_map_copy(app
),
823 app
= set_path_length(app
, 0, 1);
831 /* Call construct_component and, if "project" is set, project out
832 * the final coordinates.
834 static __isl_give isl_map
*construct_projected_component(
835 __isl_take isl_dim
*dim
,
836 __isl_keep isl_map
*map
, int *exact
, int project
)
843 d
= isl_dim_size(dim
, isl_dim_in
);
845 app
= construct_component(dim
, map
, exact
, project
);
847 app
= isl_map_project_out(app
, isl_dim_in
, d
- 1, 1);
848 app
= isl_map_project_out(app
, isl_dim_out
, d
- 1, 1);
853 /* Compute an extended version, i.e., with path lengths, of
854 * an overapproximation of the transitive closure of "bmap"
855 * with path lengths greater than or equal to zero and with
856 * domain and range equal to "dom".
858 static __isl_give isl_map
*q_closure(__isl_take isl_dim
*dim
,
859 __isl_take isl_set
*dom
, __isl_keep isl_basic_map
*bmap
, int *exact
)
866 dom
= isl_set_add(dom
, isl_dim_set
, 1);
867 app
= isl_map_from_domain_and_range(dom
, isl_set_copy(dom
));
868 map
= isl_map_from_basic_map(isl_basic_map_copy(bmap
));
869 path
= construct_extended_path(dim
, map
, &project
);
870 app
= isl_map_intersect(app
, path
);
872 if ((*exact
= check_exactness(map
, isl_map_copy(app
), project
)) < 0)
881 /* Check whether qc has any elements of length at least one
882 * with domain and/or range outside of dom and ran.
884 static int has_spurious_elements(__isl_keep isl_map
*qc
,
885 __isl_keep isl_set
*dom
, __isl_keep isl_set
*ran
)
891 if (!qc
|| !dom
|| !ran
)
894 d
= isl_map_dim(qc
, isl_dim_in
);
896 qc
= isl_map_copy(qc
);
897 qc
= set_path_length(qc
, 0, 1);
898 qc
= isl_map_project_out(qc
, isl_dim_in
, d
- 1, 1);
899 qc
= isl_map_project_out(qc
, isl_dim_out
, d
- 1, 1);
901 s
= isl_map_domain(isl_map_copy(qc
));
902 subset
= isl_set_is_subset(s
, dom
);
911 s
= isl_map_range(qc
);
912 subset
= isl_set_is_subset(s
, ran
);
915 return subset
< 0 ? -1 : !subset
;
924 /* For each basic map in "map", except i, check whether it combines
925 * with the transitive closure that is reflexive on C combines
926 * to the left and to the right.
930 * dom map_j \subseteq C
932 * then right[j] is set to 1. Otherwise, if
934 * ran map_i \cap dom map_j = \emptyset
936 * then right[j] is set to 0. Otherwise, composing to the right
939 * Similar, for composing to the left, we have if
941 * ran map_j \subseteq C
943 * then left[j] is set to 1. Otherwise, if
945 * dom map_i \cap ran map_j = \emptyset
947 * then left[j] is set to 0. Otherwise, composing to the left
950 * The return value is or'd with LEFT if composing to the left
951 * is possible and with RIGHT if composing to the right is possible.
953 static int composability(__isl_keep isl_set
*C
, int i
,
954 isl_set
**dom
, isl_set
**ran
, int *left
, int *right
,
955 __isl_keep isl_map
*map
)
961 for (j
= 0; j
< map
->n
&& ok
; ++j
) {
962 int overlaps
, subset
;
968 dom
[j
] = isl_set_from_basic_set(
969 isl_basic_map_domain(
970 isl_basic_map_copy(map
->p
[j
])));
973 overlaps
= isl_set_overlaps(ran
[i
], dom
[j
]);
979 subset
= isl_set_is_subset(dom
[j
], C
);
991 ran
[j
] = isl_set_from_basic_set(
993 isl_basic_map_copy(map
->p
[j
])));
996 overlaps
= isl_set_overlaps(dom
[i
], ran
[j
]);
1002 subset
= isl_set_is_subset(ran
[j
], C
);
1016 /* Return a map that is a union of the basic maps in "map", except i,
1017 * composed to left and right with qc based on the entries of "left"
1020 static __isl_give isl_map
*compose(__isl_keep isl_map
*map
, int i
,
1021 __isl_take isl_map
*qc
, int *left
, int *right
)
1026 comp
= isl_map_empty(isl_map_get_dim(map
));
1027 for (j
= 0; j
< map
->n
; ++j
) {
1033 map_j
= isl_map_from_basic_map(isl_basic_map_copy(map
->p
[j
]));
1034 if (left
&& left
[j
])
1035 map_j
= isl_map_apply_range(map_j
, isl_map_copy(qc
));
1036 if (right
&& right
[j
])
1037 map_j
= isl_map_apply_range(isl_map_copy(qc
), map_j
);
1038 comp
= isl_map_union(comp
, map_j
);
1041 comp
= isl_map_compute_divs(comp
);
1042 comp
= isl_map_coalesce(comp
);
1049 /* Compute the transitive closure of "map" incrementally by
1056 * map_i^+ \cup ((id \cup map_i^) \circ qc^+)
1060 * map_i^+ \cup (qc^+ \circ (id \cup map_i^))
1062 * depending on whether left or right are NULL.
1064 static __isl_give isl_map
*compute_incremental(
1065 __isl_take isl_dim
*dim
, __isl_keep isl_map
*map
,
1066 int i
, __isl_take isl_map
*qc
, int *left
, int *right
, int *exact
)
1070 isl_map
*rtc
= NULL
;
1074 isl_assert(map
->ctx
, left
|| right
, goto error
);
1076 map_i
= isl_map_from_basic_map(isl_basic_map_copy(map
->p
[i
]));
1077 tc
= construct_projected_component(isl_dim_copy(dim
), map_i
,
1079 isl_map_free(map_i
);
1082 qc
= isl_map_transitive_closure(qc
, exact
);
1088 return isl_map_universe(isl_map_get_dim(map
));
1091 if (!left
|| !right
)
1092 rtc
= isl_map_union(isl_map_copy(tc
),
1093 isl_map_identity(isl_dim_domain(isl_map_get_dim(tc
))));
1095 qc
= isl_map_apply_range(rtc
, qc
);
1097 qc
= isl_map_apply_range(qc
, rtc
);
1098 qc
= isl_map_union(tc
, qc
);
1109 /* Given a map "map", try to find a basic map such that
1110 * map^+ can be computed as
1112 * map^+ = map_i^+ \cup
1113 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1115 * with C the simple hull of the domain and range of the input map.
1116 * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
1117 * and by intersecting domain and range with C.
1118 * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
1119 * Also, we only use the incremental computation if all the transitive
1120 * closures are exact and if the number of basic maps in the union,
1121 * after computing the integer divisions, is smaller than the number
1122 * of basic maps in the input map.
1124 static int incemental_on_entire_domain(__isl_keep isl_dim
*dim
,
1125 __isl_keep isl_map
*map
,
1126 isl_set
**dom
, isl_set
**ran
, int *left
, int *right
,
1127 __isl_give isl_map
**res
)
1135 C
= isl_set_union(isl_map_domain(isl_map_copy(map
)),
1136 isl_map_range(isl_map_copy(map
)));
1137 C
= isl_set_from_basic_set(isl_set_simple_hull(C
));
1145 d
= isl_map_dim(map
, isl_dim_in
);
1147 for (i
= 0; i
< map
->n
; ++i
) {
1149 int exact_i
, spurious
;
1151 dom
[i
] = isl_set_from_basic_set(isl_basic_map_domain(
1152 isl_basic_map_copy(map
->p
[i
])));
1153 ran
[i
] = isl_set_from_basic_set(isl_basic_map_range(
1154 isl_basic_map_copy(map
->p
[i
])));
1155 qc
= q_closure(isl_dim_copy(dim
), isl_set_copy(C
),
1156 map
->p
[i
], &exact_i
);
1163 spurious
= has_spurious_elements(qc
, dom
[i
], ran
[i
]);
1170 qc
= isl_map_project_out(qc
, isl_dim_in
, d
, 1);
1171 qc
= isl_map_project_out(qc
, isl_dim_out
, d
, 1);
1172 qc
= isl_map_compute_divs(qc
);
1173 for (j
= 0; j
< map
->n
; ++j
)
1174 left
[j
] = right
[j
] = 1;
1175 qc
= compose(map
, i
, qc
, left
, right
);
1178 if (qc
->n
>= map
->n
) {
1182 *res
= compute_incremental(isl_dim_copy(dim
), map
, i
, qc
,
1183 left
, right
, &exact_i
);
1194 return *res
!= NULL
;
1200 /* Try and compute the transitive closure of "map" as
1202 * map^+ = map_i^+ \cup
1203 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1205 * with C either the simple hull of the domain and range of the entire
1206 * map or the simple hull of domain and range of map_i.
1208 static __isl_give isl_map
*incremental_closure(__isl_take isl_dim
*dim
,
1209 __isl_keep isl_map
*map
, int *exact
, int project
)
1212 isl_set
**dom
= NULL
;
1213 isl_set
**ran
= NULL
;
1218 isl_map
*res
= NULL
;
1221 return construct_projected_component(dim
, map
, exact
, project
);
1226 return construct_projected_component(dim
, map
, exact
, project
);
1228 d
= isl_map_dim(map
, isl_dim_in
);
1230 dom
= isl_calloc_array(map
->ctx
, isl_set
*, map
->n
);
1231 ran
= isl_calloc_array(map
->ctx
, isl_set
*, map
->n
);
1232 left
= isl_calloc_array(map
->ctx
, int, map
->n
);
1233 right
= isl_calloc_array(map
->ctx
, int, map
->n
);
1234 if (!ran
|| !dom
|| !left
|| !right
)
1237 if (incemental_on_entire_domain(dim
, map
, dom
, ran
, left
, right
, &res
) < 0)
1240 for (i
= 0; !res
&& i
< map
->n
; ++i
) {
1242 int exact_i
, spurious
, comp
;
1244 dom
[i
] = isl_set_from_basic_set(
1245 isl_basic_map_domain(
1246 isl_basic_map_copy(map
->p
[i
])));
1250 ran
[i
] = isl_set_from_basic_set(
1251 isl_basic_map_range(
1252 isl_basic_map_copy(map
->p
[i
])));
1255 C
= isl_set_union(isl_set_copy(dom
[i
]),
1256 isl_set_copy(ran
[i
]));
1257 C
= isl_set_from_basic_set(isl_set_simple_hull(C
));
1264 comp
= composability(C
, i
, dom
, ran
, left
, right
, map
);
1265 if (!comp
|| comp
< 0) {
1271 qc
= q_closure(isl_dim_copy(dim
), C
, map
->p
[i
], &exact_i
);
1278 spurious
= has_spurious_elements(qc
, dom
[i
], ran
[i
]);
1285 qc
= isl_map_project_out(qc
, isl_dim_in
, d
, 1);
1286 qc
= isl_map_project_out(qc
, isl_dim_out
, d
, 1);
1287 qc
= isl_map_compute_divs(qc
);
1288 qc
= compose(map
, i
, qc
, (comp
& LEFT
) ? left
: NULL
,
1289 (comp
& RIGHT
) ? right
: NULL
);
1292 if (qc
->n
>= map
->n
) {
1296 res
= compute_incremental(isl_dim_copy(dim
), map
, i
, qc
,
1297 (comp
& LEFT
) ? left
: NULL
,
1298 (comp
& RIGHT
) ? right
: NULL
, &exact_i
);
1307 for (i
= 0; i
< map
->n
; ++i
) {
1308 isl_set_free(dom
[i
]);
1309 isl_set_free(ran
[i
]);
1321 return construct_projected_component(dim
, map
, exact
, project
);
1324 for (i
= 0; i
< map
->n
; ++i
)
1325 isl_set_free(dom
[i
]);
1328 for (i
= 0; i
< map
->n
; ++i
)
1329 isl_set_free(ran
[i
]);
1337 /* Given an array of sets "set", add "dom" at position "pos"
1338 * and search for elements at earlier positions that overlap with "dom".
1339 * If any can be found, then merge all of them, together with "dom", into
1340 * a single set and assign the union to the first in the array,
1341 * which becomes the new group leader for all groups involved in the merge.
1342 * During the search, we only consider group leaders, i.e., those with
1343 * group[i] = i, as the other sets have already been combined
1344 * with one of the group leaders.
1346 static int merge(isl_set
**set
, int *group
, __isl_take isl_set
*dom
, int pos
)
1351 set
[pos
] = isl_set_copy(dom
);
1353 for (i
= pos
- 1; i
>= 0; --i
) {
1359 o
= isl_set_overlaps(set
[i
], dom
);
1365 set
[i
] = isl_set_union(set
[i
], set
[group
[pos
]]);
1368 set
[group
[pos
]] = NULL
;
1369 group
[group
[pos
]] = i
;
1380 /* Given a partition of the domains and ranges of the basic maps in "map",
1381 * apply the Floyd-Warshall algorithm with the elements in the partition
1384 * In particular, there are "n" elements in the partition and "group" is
1385 * an array of length 2 * map->n with entries in [0,n-1].
1387 * We first construct a matrix of relations based on the partition information,
1388 * apply Floyd-Warshall on this matrix of relations and then take the
1389 * union of all entries in the matrix as the final result.
1391 * The algorithm iterates over all vertices. In each step, the whole
1392 * matrix is updated to include all paths that go to the current vertex,
1393 * possibly stay there a while (including passing through earlier vertices)
1394 * and then come back. At the start of each iteration, the diagonal
1395 * element corresponding to the current vertex is replaced by its
1396 * transitive closure to account for all indirect paths that stay
1397 * in the current vertex.
1399 static __isl_give isl_map
*floyd_warshall_with_groups(__isl_take isl_dim
*dim
,
1400 __isl_keep isl_map
*map
, int *exact
, int project
, int *group
, int n
)
1404 isl_map
***grid
= NULL
;
1412 return incremental_closure(dim
, map
, exact
, project
);
1415 grid
= isl_calloc_array(map
->ctx
, isl_map
**, n
);
1418 for (i
= 0; i
< n
; ++i
) {
1419 grid
[i
] = isl_calloc_array(map
->ctx
, isl_map
*, n
);
1422 for (j
= 0; j
< n
; ++j
)
1423 grid
[i
][j
] = isl_map_empty(isl_map_get_dim(map
));
1426 for (k
= 0; k
< map
->n
; ++k
) {
1428 j
= group
[2 * k
+ 1];
1429 grid
[i
][j
] = isl_map_union(grid
[i
][j
],
1430 isl_map_from_basic_map(
1431 isl_basic_map_copy(map
->p
[k
])));
1434 for (r
= 0; r
< n
; ++r
) {
1436 grid
[r
][r
] = isl_map_transitive_closure(grid
[r
][r
],
1437 (exact
&& *exact
) ? &r_exact
: NULL
);
1438 if (exact
&& *exact
&& !r_exact
)
1441 for (p
= 0; p
< n
; ++p
)
1442 for (q
= 0; q
< n
; ++q
) {
1444 if (p
== r
&& q
== r
)
1446 loop
= isl_map_apply_range(
1447 isl_map_copy(grid
[p
][r
]),
1448 isl_map_copy(grid
[r
][q
]));
1449 grid
[p
][q
] = isl_map_union(grid
[p
][q
], loop
);
1450 loop
= isl_map_apply_range(
1451 isl_map_copy(grid
[p
][r
]),
1452 isl_map_apply_range(
1453 isl_map_copy(grid
[r
][r
]),
1454 isl_map_copy(grid
[r
][q
])));
1455 grid
[p
][q
] = isl_map_union(grid
[p
][q
], loop
);
1456 grid
[p
][q
] = isl_map_coalesce(grid
[p
][q
]);
1460 app
= isl_map_empty(isl_map_get_dim(map
));
1462 for (i
= 0; i
< n
; ++i
) {
1463 for (j
= 0; j
< n
; ++j
)
1464 app
= isl_map_union(app
, grid
[i
][j
]);
1475 for (i
= 0; i
< n
; ++i
) {
1478 for (j
= 0; j
< n
; ++j
)
1479 isl_map_free(grid
[i
][j
]);
1488 /* Check if the domains and ranges of the basic maps in "map" can
1489 * be partitioned, and if so, apply Floyd-Warshall on the elements
1490 * of the partition. Note that we can only apply this algorithm
1491 * if we want to compute the transitive closure, i.e., when "project"
1492 * is set. If we want to compute the power, we need to keep track
1493 * of the lengths and the recursive calls inside the Floyd-Warshall
1494 * would result in non-linear lengths.
1496 * To find the partition, we simply consider all of the domains
1497 * and ranges in turn and combine those that overlap.
1498 * "set" contains the partition elements and "group" indicates
1499 * to which partition element a given domain or range belongs.
1500 * The domain of basic map i corresponds to element 2 * i in these arrays,
1501 * while the domain corresponds to element 2 * i + 1.
1502 * During the construction group[k] is either equal to k,
1503 * in which case set[k] contains the union of all the domains and
1504 * ranges in the corresponding group, or is equal to some l < k,
1505 * with l another domain or range in the same group.
1507 static __isl_give isl_map
*floyd_warshall(__isl_take isl_dim
*dim
,
1508 __isl_keep isl_map
*map
, int *exact
, int project
)
1511 isl_set
**set
= NULL
;
1517 if (!project
|| map
->n
<= 1)
1518 return incremental_closure(dim
, map
, exact
, project
);
1520 set
= isl_calloc_array(map
->ctx
, isl_set
*, 2 * map
->n
);
1521 group
= isl_alloc_array(map
->ctx
, int, 2 * map
->n
);
1526 for (i
= 0; i
< map
->n
; ++i
) {
1528 dom
= isl_set_from_basic_set(isl_basic_map_domain(
1529 isl_basic_map_copy(map
->p
[i
])));
1530 if (merge(set
, group
, dom
, 2 * i
) < 0)
1532 dom
= isl_set_from_basic_set(isl_basic_map_range(
1533 isl_basic_map_copy(map
->p
[i
])));
1534 if (merge(set
, group
, dom
, 2 * i
+ 1) < 0)
1539 for (i
= 0; i
< 2 * map
->n
; ++i
)
1543 group
[i
] = group
[group
[i
]];
1545 for (i
= 0; i
< 2 * map
->n
; ++i
)
1546 isl_set_free(set
[i
]);
1550 return floyd_warshall_with_groups(dim
, map
, exact
, project
, group
, n
);
1552 for (i
= 0; i
< 2 * map
->n
; ++i
)
1553 isl_set_free(set
[i
]);
1560 /* Structure for representing the nodes in the graph being traversed
1561 * using Tarjan's algorithm.
1562 * index represents the order in which nodes are visited.
1563 * min_index is the index of the root of a (sub)component.
1564 * on_stack indicates whether the node is currently on the stack.
1566 struct basic_map_sort_node
{
1571 /* Structure for representing the graph being traversed
1572 * using Tarjan's algorithm.
1573 * len is the number of nodes
1574 * node is an array of nodes
1575 * stack contains the nodes on the path from the root to the current node
1576 * sp is the stack pointer
1577 * index is the index of the last node visited
1578 * order contains the elements of the components separated by -1
1579 * op represents the current position in order
1581 * check_closed is set if we may have used the fact that
1582 * a pair of basic maps can be interchanged
1584 struct basic_map_sort
{
1586 struct basic_map_sort_node
*node
;
1595 static void basic_map_sort_free(struct basic_map_sort
*s
)
1605 static struct basic_map_sort
*basic_map_sort_alloc(struct isl_ctx
*ctx
, int len
)
1607 struct basic_map_sort
*s
;
1610 s
= isl_calloc_type(ctx
, struct basic_map_sort
);
1614 s
->node
= isl_alloc_array(ctx
, struct basic_map_sort_node
, len
);
1617 for (i
= 0; i
< len
; ++i
)
1618 s
->node
[i
].index
= -1;
1619 s
->stack
= isl_alloc_array(ctx
, int, len
);
1622 s
->order
= isl_alloc_array(ctx
, int, 2 * len
);
1630 s
->check_closed
= 0;
1634 basic_map_sort_free(s
);
1638 /* Check whether in the computation of the transitive closure
1639 * "bmap1" (R_1) should follow (or be part of the same component as)
1642 * That is check whether
1650 * If so, then there is no reason for R_1 to immediately follow R_2
1653 * *check_closed is set if the subset relation holds while
1654 * R_1 \circ R_2 is not empty.
1656 static int basic_map_follows(__isl_keep isl_basic_map
*bmap1
,
1657 __isl_keep isl_basic_map
*bmap2
, int *check_closed
)
1659 struct isl_map
*map12
= NULL
;
1660 struct isl_map
*map21
= NULL
;
1663 map21
= isl_map_from_basic_map(
1664 isl_basic_map_apply_range(
1665 isl_basic_map_copy(bmap2
),
1666 isl_basic_map_copy(bmap1
)));
1667 subset
= isl_map_is_empty(map21
);
1671 isl_map_free(map21
);
1675 map12
= isl_map_from_basic_map(
1676 isl_basic_map_apply_range(
1677 isl_basic_map_copy(bmap1
),
1678 isl_basic_map_copy(bmap2
)));
1680 subset
= isl_map_is_subset(map21
, map12
);
1682 isl_map_free(map12
);
1683 isl_map_free(map21
);
1688 return subset
< 0 ? -1 : !subset
;
1690 isl_map_free(map21
);
1694 /* Perform Tarjan's algorithm for computing the strongly connected components
1695 * in the graph with the disjuncts of "map" as vertices and with an
1696 * edge between any pair of disjuncts such that the first has
1697 * to be applied after the second.
1699 static int power_components_tarjan(struct basic_map_sort
*s
,
1700 __isl_keep isl_map
*map
, int i
)
1704 s
->node
[i
].index
= s
->index
;
1705 s
->node
[i
].min_index
= s
->index
;
1706 s
->node
[i
].on_stack
= 1;
1708 s
->stack
[s
->sp
++] = i
;
1710 for (j
= s
->len
- 1; j
>= 0; --j
) {
1715 if (s
->node
[j
].index
>= 0 &&
1716 (!s
->node
[j
].on_stack
||
1717 s
->node
[j
].index
> s
->node
[i
].min_index
))
1720 f
= basic_map_follows(map
->p
[i
], map
->p
[j
], &s
->check_closed
);
1726 if (s
->node
[j
].index
< 0) {
1727 power_components_tarjan(s
, map
, j
);
1728 if (s
->node
[j
].min_index
< s
->node
[i
].min_index
)
1729 s
->node
[i
].min_index
= s
->node
[j
].min_index
;
1730 } else if (s
->node
[j
].index
< s
->node
[i
].min_index
)
1731 s
->node
[i
].min_index
= s
->node
[j
].index
;
1734 if (s
->node
[i
].index
!= s
->node
[i
].min_index
)
1738 j
= s
->stack
[--s
->sp
];
1739 s
->node
[j
].on_stack
= 0;
1740 s
->order
[s
->op
++] = j
;
1742 s
->order
[s
->op
++] = -1;
1747 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1748 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1749 * construct a map that is an overapproximation of the map
1750 * that takes an element from the dom R \times Z to an
1751 * element from ran R \times Z, such that the first n coordinates of the
1752 * difference between them is a sum of differences between images
1753 * and pre-images in one of the R_i and such that the last coordinate
1754 * is equal to the number of steps taken.
1755 * If "project" is set, then these final coordinates are not included,
1756 * i.e., a relation of type Z^n -> Z^n is returned.
1759 * \Delta_i = { y - x | (x, y) in R_i }
1761 * then the constructed map is an overapproximation of
1763 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1764 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
1765 * x in dom R and x + d in ran R }
1769 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1770 * d = (\sum_i k_i \delta_i) and
1771 * x in dom R and x + d in ran R }
1773 * if "project" is set.
1775 * We first split the map into strongly connected components, perform
1776 * the above on each component and then join the results in the correct
1777 * order, at each join also taking in the union of both arguments
1778 * to allow for paths that do not go through one of the two arguments.
1780 static __isl_give isl_map
*construct_power_components(__isl_take isl_dim
*dim
,
1781 __isl_keep isl_map
*map
, int *exact
, int project
)
1784 struct isl_map
*path
= NULL
;
1785 struct basic_map_sort
*s
= NULL
;
1792 return floyd_warshall(dim
, map
, exact
, project
);
1794 s
= basic_map_sort_alloc(map
->ctx
, map
->n
);
1797 for (i
= map
->n
- 1; i
>= 0; --i
) {
1798 if (s
->node
[i
].index
>= 0)
1800 if (power_components_tarjan(s
, map
, i
) < 0)
1805 if (s
->check_closed
&& !exact
)
1806 exact
= &local_exact
;
1812 path
= isl_map_empty(isl_map_get_dim(map
));
1814 path
= isl_map_empty(isl_dim_copy(dim
));
1816 struct isl_map
*comp
;
1817 isl_map
*path_comp
, *path_comb
;
1818 comp
= isl_map_alloc_dim(isl_map_get_dim(map
), n
, 0);
1819 while (s
->order
[i
] != -1) {
1820 comp
= isl_map_add_basic_map(comp
,
1821 isl_basic_map_copy(map
->p
[s
->order
[i
]]));
1825 path_comp
= floyd_warshall(isl_dim_copy(dim
),
1826 comp
, exact
, project
);
1827 path_comb
= isl_map_apply_range(isl_map_copy(path
),
1828 isl_map_copy(path_comp
));
1829 path
= isl_map_union(path
, path_comp
);
1830 path
= isl_map_union(path
, path_comb
);
1836 if (c
> 1 && s
->check_closed
&& !*exact
) {
1839 closed
= isl_map_is_transitively_closed(path
);
1843 basic_map_sort_free(s
);
1845 return floyd_warshall(dim
, map
, orig_exact
, project
);
1849 basic_map_sort_free(s
);
1854 basic_map_sort_free(s
);
1860 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1861 * construct a map that is an overapproximation of the map
1862 * that takes an element from the space D to another
1863 * element from the same space, such that the difference between
1864 * them is a strictly positive sum of differences between images
1865 * and pre-images in one of the R_i.
1866 * The number of differences in the sum is equated to parameter "param".
1869 * \Delta_i = { y - x | (x, y) in R_i }
1871 * then the constructed map is an overapproximation of
1873 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1874 * d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1877 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1878 * d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1880 * if "project" is set.
1882 * If "project" is not set, then
1883 * we first construct an extended mapping with an extra coordinate
1884 * that indicates the number of steps taken. In particular,
1885 * the difference in the last coordinate is equal to the number
1886 * of steps taken to move from a domain element to the corresponding
1888 * In the final step, this difference is equated to the parameter "param"
1889 * and made positive. The extra coordinates are subsequently projected out.
1891 static __isl_give isl_map
*construct_power(__isl_keep isl_map
*map
,
1892 unsigned param
, int *exact
, int project
)
1894 struct isl_map
*app
= NULL
;
1895 struct isl_map
*diff
;
1896 struct isl_dim
*dim
= NULL
;
1902 dim
= isl_map_get_dim(map
);
1904 d
= isl_dim_size(dim
, isl_dim_in
);
1905 dim
= isl_dim_add(dim
, isl_dim_in
, 1);
1906 dim
= isl_dim_add(dim
, isl_dim_out
, 1);
1908 app
= construct_power_components(isl_dim_copy(dim
), map
,
1914 diff
= equate_parameter_to_length(dim
, param
);
1915 app
= isl_map_intersect(app
, diff
);
1916 app
= isl_map_project_out(app
, isl_dim_in
, d
, 1);
1917 app
= isl_map_project_out(app
, isl_dim_out
, d
, 1);
1923 /* Compute the positive powers of "map", or an overapproximation.
1924 * The power is given by parameter "param". If the result is exact,
1925 * then *exact is set to 1.
1927 * If project is set, then we are actually interested in the transitive
1928 * closure, so we can use a more relaxed exactness check.
1929 * The lengths of the paths are also projected out instead of being
1930 * equated to "param" (which is then ignored in this case).
1932 static __isl_give isl_map
*map_power(__isl_take isl_map
*map
, unsigned param
,
1933 int *exact
, int project
)
1935 struct isl_map
*app
= NULL
;
1943 if (isl_map_fast_is_empty(map
))
1946 isl_assert(map
->ctx
, project
|| param
< isl_map_dim(map
, isl_dim_param
),
1948 isl_assert(map
->ctx
,
1949 isl_map_dim(map
, isl_dim_in
) == isl_map_dim(map
, isl_dim_out
),
1952 app
= construct_power(map
, param
, exact
, project
);
1962 /* Compute the positive powers of "map", or an overapproximation.
1963 * The power is given by parameter "param". If the result is exact,
1964 * then *exact is set to 1.
1966 __isl_give isl_map
*isl_map_power(__isl_take isl_map
*map
, unsigned param
,
1969 map
= isl_map_compute_divs(map
);
1970 map
= isl_map_coalesce(map
);
1971 return map_power(map
, param
, exact
, 0);
1974 /* Check whether equality i of bset is a pure stride constraint
1975 * on a single dimensions, i.e., of the form
1979 * with k a constant and e an existentially quantified variable.
1981 static int is_eq_stride(__isl_keep isl_basic_set
*bset
, int i
)
1993 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1996 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1997 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1998 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
2000 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
2002 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
2005 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
2006 d
- pos1
- 1) != -1)
2009 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
2012 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
2013 n_div
- pos2
- 1) != -1)
2015 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
2016 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
2022 /* Given a map, compute the smallest superset of this map that is of the form
2024 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2026 * (where p ranges over the (non-parametric) dimensions),
2027 * compute the transitive closure of this map, i.e.,
2029 * { i -> j : exists k > 0:
2030 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2032 * and intersect domain and range of this transitive closure with
2033 * the given domain and range.
2035 * If with_id is set, then try to include as much of the identity mapping
2036 * as possible, by computing
2038 * { i -> j : exists k >= 0:
2039 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2041 * instead (i.e., allow k = 0).
2043 * In practice, we compute the difference set
2045 * delta = { j - i | i -> j in map },
2047 * look for stride constraint on the individual dimensions and compute
2048 * (constant) lower and upper bounds for each individual dimension,
2049 * adding a constraint for each bound not equal to infinity.
2051 static __isl_give isl_map
*box_closure_on_domain(__isl_take isl_map
*map
,
2052 __isl_take isl_set
*dom
, __isl_take isl_set
*ran
, int with_id
)
2061 isl_map
*app
= NULL
;
2062 isl_basic_set
*aff
= NULL
;
2063 isl_basic_map
*bmap
= NULL
;
2064 isl_vec
*obj
= NULL
;
2069 delta
= isl_map_deltas(isl_map_copy(map
));
2071 aff
= isl_set_affine_hull(isl_set_copy(delta
));
2074 dim
= isl_map_get_dim(map
);
2075 d
= isl_dim_size(dim
, isl_dim_in
);
2076 nparam
= isl_dim_size(dim
, isl_dim_param
);
2077 total
= isl_dim_total(dim
);
2078 bmap
= isl_basic_map_alloc_dim(dim
,
2079 aff
->n_div
+ 1, aff
->n_div
, 2 * d
+ 1);
2080 for (i
= 0; i
< aff
->n_div
+ 1; ++i
) {
2081 k
= isl_basic_map_alloc_div(bmap
);
2084 isl_int_set_si(bmap
->div
[k
][0], 0);
2086 for (i
= 0; i
< aff
->n_eq
; ++i
) {
2087 if (!is_eq_stride(aff
, i
))
2089 k
= isl_basic_map_alloc_equality(bmap
);
2092 isl_seq_clr(bmap
->eq
[k
], 1 + nparam
);
2093 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ d
,
2094 aff
->eq
[i
] + 1 + nparam
, d
);
2095 isl_seq_neg(bmap
->eq
[k
] + 1 + nparam
,
2096 aff
->eq
[i
] + 1 + nparam
, d
);
2097 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ 2 * d
,
2098 aff
->eq
[i
] + 1 + nparam
+ d
, aff
->n_div
);
2099 isl_int_set_si(bmap
->eq
[k
][1 + total
+ aff
->n_div
], 0);
2101 obj
= isl_vec_alloc(map
->ctx
, 1 + nparam
+ d
);
2104 isl_seq_clr(obj
->el
, 1 + nparam
+ d
);
2105 for (i
= 0; i
< d
; ++ i
) {
2106 enum isl_lp_result res
;
2108 isl_int_set_si(obj
->el
[1 + nparam
+ i
], 1);
2110 res
= isl_set_solve_lp(delta
, 0, obj
->el
, map
->ctx
->one
, &opt
,
2112 if (res
== isl_lp_error
)
2114 if (res
== isl_lp_ok
) {
2115 k
= isl_basic_map_alloc_inequality(bmap
);
2118 isl_seq_clr(bmap
->ineq
[k
],
2119 1 + nparam
+ 2 * d
+ bmap
->n_div
);
2120 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ i
], -1);
2121 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ d
+ i
], 1);
2122 isl_int_neg(bmap
->ineq
[k
][1 + nparam
+ 2 * d
+ aff
->n_div
], opt
);
2125 res
= isl_set_solve_lp(delta
, 1, obj
->el
, map
->ctx
->one
, &opt
,
2127 if (res
== isl_lp_error
)
2129 if (res
== isl_lp_ok
) {
2130 k
= isl_basic_map_alloc_inequality(bmap
);
2133 isl_seq_clr(bmap
->ineq
[k
],
2134 1 + nparam
+ 2 * d
+ bmap
->n_div
);
2135 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ i
], 1);
2136 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ d
+ i
], -1);
2137 isl_int_set(bmap
->ineq
[k
][1 + nparam
+ 2 * d
+ aff
->n_div
], opt
);
2140 isl_int_set_si(obj
->el
[1 + nparam
+ i
], 0);
2142 k
= isl_basic_map_alloc_inequality(bmap
);
2145 isl_seq_clr(bmap
->ineq
[k
],
2146 1 + nparam
+ 2 * d
+ bmap
->n_div
);
2148 isl_int_set_si(bmap
->ineq
[k
][0], -1);
2149 isl_int_set_si(bmap
->ineq
[k
][1 + nparam
+ 2 * d
+ aff
->n_div
], 1);
2151 app
= isl_map_from_domain_and_range(dom
, ran
);
2154 isl_basic_set_free(aff
);
2156 bmap
= isl_basic_map_finalize(bmap
);
2157 isl_set_free(delta
);
2160 map
= isl_map_from_basic_map(bmap
);
2161 map
= isl_map_intersect(map
, app
);
2166 isl_basic_map_free(bmap
);
2167 isl_basic_set_free(aff
);
2171 isl_set_free(delta
);
2176 /* Given a map, compute the smallest superset of this map that is of the form
2178 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2180 * (where p ranges over the (non-parametric) dimensions),
2181 * compute the transitive closure of this map, i.e.,
2183 * { i -> j : exists k > 0:
2184 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2186 * and intersect domain and range of this transitive closure with
2187 * domain and range of the original map.
2189 static __isl_give isl_map
*box_closure(__isl_take isl_map
*map
)
2194 domain
= isl_map_domain(isl_map_copy(map
));
2195 domain
= isl_set_coalesce(domain
);
2196 range
= isl_map_range(isl_map_copy(map
));
2197 range
= isl_set_coalesce(range
);
2199 return box_closure_on_domain(map
, domain
, range
, 0);
2202 /* Given a map, compute the smallest superset of this map that is of the form
2204 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2206 * (where p ranges over the (non-parametric) dimensions),
2207 * compute the transitive and partially reflexive closure of this map, i.e.,
2209 * { i -> j : exists k >= 0:
2210 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2212 * and intersect domain and range of this transitive closure with
2215 static __isl_give isl_map
*box_closure_with_identity(__isl_take isl_map
*map
,
2216 __isl_take isl_set
*dom
)
2218 return box_closure_on_domain(map
, dom
, isl_set_copy(dom
), 1);
2221 /* Check whether app is the transitive closure of map.
2222 * In particular, check that app is acyclic and, if so,
2225 * app \subset (map \cup (map \circ app))
2227 static int check_exactness_omega(__isl_keep isl_map
*map
,
2228 __isl_keep isl_map
*app
)
2232 int is_empty
, is_exact
;
2236 delta
= isl_map_deltas(isl_map_copy(app
));
2237 d
= isl_set_dim(delta
, isl_dim_set
);
2238 for (i
= 0; i
< d
; ++i
)
2239 delta
= isl_set_fix_si(delta
, isl_dim_set
, i
, 0);
2240 is_empty
= isl_set_is_empty(delta
);
2241 isl_set_free(delta
);
2247 test
= isl_map_apply_range(isl_map_copy(app
), isl_map_copy(map
));
2248 test
= isl_map_union(test
, isl_map_copy(map
));
2249 is_exact
= isl_map_is_subset(app
, test
);
2255 /* Check if basic map M_i can be combined with all the other
2256 * basic maps such that
2260 * can be computed as
2262 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2264 * In particular, check if we can compute a compact representation
2267 * M_i^* \circ M_j \circ M_i^*
2270 * Let M_i^? be an extension of M_i^+ that allows paths
2271 * of length zero, i.e., the result of box_closure(., 1).
2272 * The criterion, as proposed by Kelly et al., is that
2273 * id = M_i^? - M_i^+ can be represented as a basic map
2276 * id \circ M_j \circ id = M_j
2280 * If this function returns 1, then tc and qc are set to
2281 * M_i^+ and M_i^?, respectively.
2283 static int can_be_split_off(__isl_keep isl_map
*map
, int i
,
2284 __isl_give isl_map
**tc
, __isl_give isl_map
**qc
)
2286 isl_map
*map_i
, *id
= NULL
;
2293 C
= isl_set_union(isl_map_domain(isl_map_copy(map
)),
2294 isl_map_range(isl_map_copy(map
)));
2295 C
= isl_set_from_basic_set(isl_set_simple_hull(C
));
2299 map_i
= isl_map_from_basic_map(isl_basic_map_copy(map
->p
[i
]));
2300 *tc
= box_closure(isl_map_copy(map_i
));
2301 *qc
= box_closure_with_identity(map_i
, C
);
2302 id
= isl_map_subtract(isl_map_copy(*qc
), isl_map_copy(*tc
));
2306 if (id
->n
!= 1 || (*qc
)->n
!= 1)
2309 for (j
= 0; j
< map
->n
; ++j
) {
2310 isl_map
*map_j
, *test
;
2315 map_j
= isl_map_from_basic_map(
2316 isl_basic_map_copy(map
->p
[j
]));
2317 test
= isl_map_apply_range(isl_map_copy(id
),
2318 isl_map_copy(map_j
));
2319 test
= isl_map_apply_range(test
, isl_map_copy(id
));
2320 is_ok
= isl_map_is_equal(test
, map_j
);
2321 isl_map_free(map_j
);
2349 static __isl_give isl_map
*box_closure_with_check(__isl_take isl_map
*map
,
2354 app
= box_closure(isl_map_copy(map
));
2356 *exact
= check_exactness_omega(map
, app
);
2362 /* Compute an overapproximation of the transitive closure of "map"
2363 * using a variation of the algorithm from
2364 * "Transitive Closure of Infinite Graphs and its Applications"
2367 * We first check whether we can can split of any basic map M_i and
2374 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2376 * using a recursive call on the remaining map.
2378 * If not, we simply call box_closure on the whole map.
2380 static __isl_give isl_map
*transitive_closure_omega(__isl_take isl_map
*map
,
2390 return box_closure_with_check(map
, exact
);
2392 for (i
= 0; i
< map
->n
; ++i
) {
2395 ok
= can_be_split_off(map
, i
, &tc
, &qc
);
2401 app
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
- 1, 0);
2403 for (j
= 0; j
< map
->n
; ++j
) {
2406 app
= isl_map_add_basic_map(app
,
2407 isl_basic_map_copy(map
->p
[j
]));
2410 app
= isl_map_apply_range(isl_map_copy(qc
), app
);
2411 app
= isl_map_apply_range(app
, qc
);
2413 app
= isl_map_union(tc
, transitive_closure_omega(app
, NULL
));
2414 exact_i
= check_exactness_omega(map
, app
);
2426 return box_closure_with_check(map
, exact
);
2432 /* Compute the transitive closure of "map", or an overapproximation.
2433 * If the result is exact, then *exact is set to 1.
2434 * Simply use map_power to compute the powers of map, but tell
2435 * it to project out the lengths of the paths instead of equating
2436 * the length to a parameter.
2438 __isl_give isl_map
*isl_map_transitive_closure(__isl_take isl_map
*map
,
2447 if (map
->ctx
->opt
->closure
== ISL_CLOSURE_OMEGA
)
2448 return transitive_closure_omega(map
, exact
);
2450 map
= isl_map_compute_divs(map
);
2451 map
= isl_map_coalesce(map
);
2452 closed
= isl_map_is_transitively_closed(map
);
2461 param
= isl_map_dim(map
, isl_dim_param
);
2462 map
= map_power(map
, param
, exact
, 1);